关于UTF-8的介绍请参考这篇文章,只要按照UTF-8的格式check即可。注意虽然UTF-8目前的最大值为0x10FF,这一题不需要check,check的话过不了test case。代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool validUtf8(vector<int>& data) { | |
const int MOD = 256; | |
int i = 0, len = data.size(); | |
while (i < len) | |
{ | |
int byte = data[i] % MOD; | |
int k = 0; | |
while (k >= 0 && byte >> (7 - k) & 1)++k; | |
if (k == 1 || k > 4)return false; | |
//unicode max value 0010 FFFF | |
//if (k == 4 && byte % 8 > 4)return false; | |
while (--k > 0) | |
{ | |
++i; | |
if (i >= len)return false; | |
int nextByte = data[i] % MOD; | |
if ((nextByte >> 7 & 1) == 0 || nextByte >> 6 & 1)return false; | |
} | |
++i; | |
} | |
return true; | |
} | |
}; |
No comments:
Post a Comment