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个开关状态是好的,于是他们把这称为"字节".再后来,他们又做了一些可以处理这 ...
随机推荐
- Oracle-12541:TNS:无监听程序 .
背景:自己机子做oracle服务器,其他机子可以ping得通我的机子,但是jdbc就是连不上,后来用plsql连出现无监听程序.... 我昨天重新安装Oracle后,用PL/SQL Developer ...
- WinForm开发中针对TreeView控件改变当前选择节点的字体与颜色
本文转载:http://www.cnblogs.com/umplatform/archive/2012/08/29/2660240.html 在B/S开发中,对TreeView控件要改变当前选中节点的 ...
- KeyTweak 键盘按键功能修改
最近一致再用ThinkPad S3,悲剧的是上翻页和下翻页竟然和方向键在一起,经常按错光标不知道去哪里了. 实在忍受不了,竟然有这样的软件,哈哈. KeyTweak,用起来太方便了
- Rational Rose--简介
Rational Rose Rational Rose是Rational公司出品的一种面向对象的统一建模语言的可视化建模工具.用于可视化建模和公司级水平软件应用的组件构造. 目前版本的Rational ...
- android118 上拉下拉刷新列表listView实现
MainActivity.java package com.heima52.pullrefresh; import java.util.ArrayList; import com.heima52.pu ...
- 《高级Perl编程》 读书笔记
http://blog.chinaunix.net/uid-20767124-id-1849881.html
- ios 入门之Hello World
1.ios系统的概述与构架ios平台限制集成开发环境介绍第一个程序-hello World应用程序的文件组织模拟器的常用操作应用程序的生命周期 CocoaTouch层UIKit框架:UIKit提供了一 ...
- debian配置简单的vsftp服务器
Ubuntu Linux与Windows系统不同,Ubuntu Linux不会产生无用垃圾文件,但是在升级缓存中,Ubuntu Linux不会自动删除这些文件,今天就来说说这些垃圾文件清理方法. 1 ...
- Android(java)学习笔记154:使用GridView以及重写BaseAdapter
1.BaseAdapter: 对于ListView.GridView.Gallery.Spinner等等,它是它们的适配器,直接继承自接口类Adapter的,使用BaseAdapter时需要重写很多方 ...
- 报错---[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294**
原因: 新的SDK不允许在设置rootViewController之前做过于复杂的操作,导致在didFinishLaunchingWithOptions 结束后还没有设置rootViewControl ...