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个开关状态是好的,于是他们把这称为"字节".再后来,他们又做了一些可以处理这 ...
随机推荐
- C++ vector的用法
其实我是一个比较懒惰的人 我最喜欢的循环方式是for(int i=0;i<length;i++) 同样也可以 for (unsigned i=0; i<sz; i++) myvector[ ...
- HDU 3452 Bonsai(网络流之最小割)
题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...
- const和readonly差别
我们都知道,const和static readonly的确非常像:通过类名而不是对象名进行訪问,在程序中仅仅读等等.在多数情况下能够混用.二者本质的差别在于,const的值是在编译期间确定的,因此仅仅 ...
- SAP增强总结-第二代增强(SMOD、CMOD)【转载】
第二代增强比第二代增强安全性提高了很多,第一代增强毕竟是在原标准程序中修改,大部分传递参数都可以直接使用,第二代增强做了一些封装,对用户可以修改的参数做了限制. 1.增强点查找方法 首先根据事物码找到 ...
- navicat for mysql (10038)如何解决,远程无法连接问题
ubuntu server下安装了MySQL 5.5数据库,然后在windows下通过Navicat for MySQL连接时,出现 Can't connect to mysql server on ...
- node.js在windows下的学习笔记(5)---用NODE.JS创建服务器和客户端
//引入http模块 var http = require('http'); //调用http的createServer的方法,这个方法有一个回调函数,这个回调数 //的作用是当有请求发送给服务器的时 ...
- WSGI、flup、fastcgi、web.py的关系
Apache/lighttpd: 相当于一个request proxy,根据配置,把不同的请求转发给不同的server处理,例如静态的文件请求自己处理,这个时候它就像一个web server,对于fa ...
- PHP之open_ssl
http://www.wapm.cn/phpdoc/zh/openssl.installation.html http://liuxufei.com/weblog/jishu/376.html dem ...
- Upgrade Guide
Upgrade Guide This guide will point out the key points to be aware of when upgrading to version 3. A ...
- 手把手教你使用UICollectionView写公司的项目
在很多app中都有这样通用的页面,一直没有机会使用UICollectionView,只是简单的看过他的使用方法.今天公司美工出图,使用了他,并且遇到了好多的坑.记录一下过程,不确定使用的方法是不是最优 ...