393. UTF-8 Validation
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的更多相关文章
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- [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 ...
- 393 UTF-8 Validation UTF-8 编码验证
详见:https://leetcode.com/problems/utf-8-validation/description/ C++: class Solution { public: bool va ...
- 【LeetCode】393. UTF-8 Validation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/utf-8-va ...
- jquery插件讲解:轮播(SlidesJs)+验证(Validation)
SlidesJs(轮播支持触屏)——官网(http://slidesjs.com) 1.简介 SlidesJs是基于Jquery(1.7.1+)的响应幻灯片插件.支持键盘,触摸,css3转换. 2.代 ...
- 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 ...
- jQuery学习之路(8)- 表单验证插件-Validation
▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...
- 从Java String实例来理解ANSI、Unicode、BMP、UTF等编码概念
转(http://www.codeceo.com/article/java-string-ansi-unicode-bmp-utf.html#0-tsina-1-10971-397232819ff9a ...
- ascii、unicode、utf、gb等编码详解
很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节".再后来,他们又做了一些可以处理这 ...
随机推荐
- 剑指OFFER之第一个只出现一次的字符(九度OJ1283)
题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据每一组输入一个字符串. 输出: 输出第一个只出现一次的字 ...
- java异步任务处理
1.场景 最近做项目的时候遇到了一个小问题:从前台提交到服务端A,A调用服务端B处理超时,原因是前端一次请求往db插1万数据,插完之后会去清理缓存.发送消息. 服务端的有三个操作 a.插DB b.清理 ...
- js 数组排序要注意的问题,返回的值最好为 -1, 0, 1之间的值
var test10Elements = [7, 6, 5, 4, 3, 2, 1, 0, 8, 9]; var comparefn = function (x, y) { return x - y; ...
- C#_MVC_Repository_CRUD_Model
using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace iFlytekDemo ...
- C#_datatable_读取
private void button5_Click(object sender, EventArgs e) { string 价格编号 = txtnum.Text; if (价格编号!= " ...
- oracle数据库中,分天查询数目
select to_CHAR(SP_MT_TIME,'DD'),count(*) from table2 group by to_CHAR(SP_MT_TIME,'DD');
- c++内联函数与静态函数
不能是虚函数的成员函数有:静态成员函数,内联成员函数,构造函数.没有什么函数需要硬性规定为虚函数,一般析构函数会被定义为虚函数其他就是在继承类中可能需要override的类成员函数应该定义为虚函数 h ...
- Java队列实现
队列数组实现:队列长度有限,但是考虑到平时一般都使用有界队列,这应该也不算是个缺点 public class Queue { private Object[] objs; private int he ...
- [转]div里table居中的问题 Div与body顶部间隙
本文转自:http://www.cnblogs.com/jinhui/archive/2008/09/24/1297729.html 将div的text-align设为center,然后将table的 ...
- Visual Studio 2015中的常用调试技巧分享
.NET 技术交流群:337901356 欢迎您的加入! 为什么要学习调试? 调试(Debug)是作为一个程序员必须要学会的东西,学会调试可以极大的提高开发效率,排错时间,很多人不喜欢调试,但我认为这 ...