393. UTF-8 Validation

这个题很明确,刚开始我以为只能是一个utf,长度大于5的都判断为false,后来才明白题意。

有个小trick,就是长度大于1的时候,判断第一个数字开始1的个数,至少要大于1才行,这个算是考虑不全。

class Solution {
public:
bool work(vector<int> &data, int x, int y) { if(x > y) return 1;
int cur = data[x];
if(!(cur & (1 << 7))) {
return work(data, x + 1, y);
} else {
int cnt = 0;
for (int i = 7; i >= 0; i--) {
if(cur & (1 << i)) cnt++;
else break;
}
//cout << cnt << endl;
if(cnt > 4 || cnt < 2) return 0;
if(y - x + 1 < cnt) return 0;
for (int i = 1; i < cnt; i++) {
cur = data[x + i];
if((cur >> 6) != 2) return 0;
}
return work(data, x + cnt, y);
}
}
bool validUtf8(vector<int>& data) {
return work(data, 0, data.size() - 1);
}
};

393. UTF-8 Validation的更多相关文章

  1. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  2. [LeetCode] 393. UTF-8 Validation 编码验证

    A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte char ...

  3. 393 UTF-8 Validation UTF-8 编码验证

    详见:https://leetcode.com/problems/utf-8-validation/description/ C++: class Solution { public: bool va ...

  4. 【LeetCode】393. UTF-8 Validation 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/utf-8-va ...

  5. jquery插件讲解:轮播(SlidesJs)+验证(Validation)

    SlidesJs(轮播支持触屏)——官网(http://slidesjs.com) 1.简介 SlidesJs是基于Jquery(1.7.1+)的响应幻灯片插件.支持键盘,触摸,css3转换. 2.代 ...

  6. LeetCode赛题393----UTF-8 Validation

    393. UTF-8 Validation A character in UTF8 can be from 1 to 4 bytes long, subjected to the following ...

  7. jQuery学习之路(8)- 表单验证插件-Validation

    ▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...

  8. 从Java String实例来理解ANSI、Unicode、BMP、UTF等编码概念

    转(http://www.codeceo.com/article/java-string-ansi-unicode-bmp-utf.html#0-tsina-1-10971-397232819ff9a ...

  9. ascii、unicode、utf、gb等编码详解

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节".再后来,他们又做了一些可以处理这 ...

随机推荐

  1. js Uncaught SyntaxError: Unexpected token错误

    今天遇到js报错Uncaught SyntaxError: Unexpected token 不知道是什么原因,并且js还会继续往下执行. 经过排查竟然是在保存行的上面有个if少一个大括号,真是坑爹啊 ...

  2. BZOJ [ZJOI2008]泡泡堂BNB 贪心

    [ZJOI2008]泡泡堂BNB Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/proble ...

  3. kvm上安装xp

    主要为了看看图像显示是否有问题,跑起来系能如何,网络连接.文件共享是怎样的. 用的是雨林木风xp sp3的iso.为了提高性能,决定使用qcow2格式,预分配metadata,cache=none(查 ...

  4. [React] Set up React apps with zero configuration

    The React team has an official Command Line Interface (CLI) for building React projects called " ...

  5. PHP中获取中英文混合字符串长度[主要是指个数,而不是字符串长度](转)

    今晚在写框架的表单验证类时,需要判断某个字符串长度是否在指定区间内,很自然地,想到了PHP中的strlen函数. $str = 'Hello world!'; echo strlen($str);   ...

  6. 深入理解C++中的mutable关键字

      mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将 ...

  7. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  8. Android 换肤功能的实现(Apk插件方式)

    一.概述 由于Android 没有提供一套统一的换肤机制,我猜可能是因为国外更注重功能和体验的原因 所以国内如果要做一个漂亮的换肤方案,需要自己去实现. 目前换肤的方法大概有三种方案: (1)把皮肤资 ...

  9. ios 界面间跳转方法总结

    接触ios也有一段时间了,偶然间,我们公司的技术总监兼我的导师,问我,你可知道,界面间的跳转有几种方式?我说出了两种,但是也有点含糊其辞,于是我就想一定要找个时间总结一下.有句话说的好,“前人种树,后 ...

  10. 网站压力测试工具webbench 安装与使用

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好用,安装使用也特别方便,并且非常小. 主要是 -t 参数用着比较爽,下面参考了张宴的文章 ...