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. CentOS6.5安装telnet

    原文地址:http://www.cnblogs.com/zhongshengzhen/ 1.检查是否已经安装telnet [root@localhost ~]# rpm -qa | grep teln ...

  2. 修改cas登陆页面-服务器端

    原文地址:http://www.cnblogs.com/liveandevil/archive/2013/03/06/2946341.html 1.cas统一认证的登陆页面位于:cas目录/WEB-I ...

  3. cc2530 timer 3 PWM <可调占空比>

    前提: 开始用的是 cc2530 timer 1来做PWM的,已经可调占空比了,但是由于硬件的改动,需要用timer 3 和 timer 4 代替.由于调试过程中出了些小问题,于是自己把这个贴出来.关 ...

  4. ThinkPHP 获取配置文件中的值

    C('SPECIAL_USER'):获取配置文件中的值 存入数组

  5. 查看Linux主机CPU及内存信息

    查看CPU信息(型号)  # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c        8  Intel(R) Xeon(R) CPU ...

  6. ExtJs 下拉菜单分页工具插件 代码分析

    Ext.ns("Ext.ux"); //创建插件对象 Ext.ux.PageSizePlugin = function(){ //调用父对象的构造方法,并为此插件生成一个预定义st ...

  7. Java基础知识强化之多线程笔记03:进程与线程 和 多线程的意义

    1. 要想了解多线程,必须先了解线程,而要想了解线程,必须先了解进程,因为线程是依赖于进程而存在. 2. 什么是进程? 通过任务管理器我们就看到了进程的存在. 而通过观察,我们发现只有运行的程序才会出 ...

  8. 按字母顺序排序的 arcpy.mapping 类列表

    arcpy.mapping 类可使用地图文档 (.mxd) 或图层文件 (.lyr) 中的不同对象类型的各种方法和属性.此文档可专门用作快速参考.有关详细信息,请使用链接跳转至各帮助页面. arcpy ...

  9. How to center a div in bootstrap3

    There are two approaches to centering a column <div> in Bootstrap 3: Approach 1 (offsets): The ...

  10. FastDFS为什么要结合Nginx

    FastDFS为什么要结合Nginx? 我们在使用FastDFS部署一个分布式文件系统的时候,通过FastDFS的客户端API来进行文件的上传.下载.删除等操作.同时通过FastDFS的HTTP服务器 ...