当前位置: 首页 > news >正文

C++ | Leetcode C++题解之第393题UFT-8编码验证

题目:

题解:

class Solution {
public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return 1;}int n = 0;int mask = MASK1;while ((num & mask) != 0) {n++;if (n > 4) {return -1;}mask >>= 1;}return n >= 2 ? n : -1;}bool validUtf8(vector<int>& data) {int m = data.size();int index = 0;while (index < m) {int num = data[index];int n = getBytes(num);if (n < 0 || index + n > m) {return false;}for (int i = 1; i < n; i++) {if (!isValid(data[index + i])) {return false;}}index += n;}return true;}
};

http://www.mrgr.cn/news/19274.html

相关文章:

  • Python | Leetcode Python题解之第393题UTF-8编码验证
  • 如何选择SSD
  • IBM企业流程框架方法论-附PPT下载
  • 查看端口被占用情况
  • 读软件开发安全之道:概念、设计与实施17读后总结与感想兼导读
  • 山东省行政执法证照片要求及图像处理方法
  • 基于web设备管理系统设计与实现
  • GPU版pytorch安装(win/linux)
  • JS中【async】和【defer】属性详解与区别
  • 4.3 Sensors -- onLongPress
  • C++ | Leetcode C++题解之第394题字符串解码
  • 数据结构之链表
  • Python Tkinter小程序
  • 分类预测|基于蜣螂优化极限梯度提升决策树的数据分类预测Matlab程序DBO-Xgboost 多特征输入单输出 含基础模型
  • 浏览器自动化测试的利器:Cypress
  • SPI总线协议详解
  • Golang | Leetcode Golang题解之第394题字符串解码
  • 高效异步编程:使用Python的asyncio库实现异步I/O操作
  • 我找到了一个让ChatGPT稳定通过草莓测试的方法,百试百灵!
  • 【conda】Conda 环境迁移指南:如何更改 envs_dirs 和 pkgs_dirs 以及跨盘迁移