-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathutf-8-validation.js
56 lines (51 loc) · 1.28 KB
/
utf-8-validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Source : https://leetcode.com/problems/utf-8-validation/
// Author : Han Zichi
// Date : 2016-09-05
/**
* @param {number[]} data
* @return {boolean}
*/
var validUtf8 = function(data) {
// 十进制转为二进制,并补前导 0(八位二进制)
function fn(num) {
let str = num.toString(2);
while (str.length !== 8)
str = '0' + str;
return str;
}
for (let i = 0, len = data.length; i < len; i++) {
let str = fn(data[i]);
if (str.startsWith('0'))
continue;
else if (str.startsWith("110")) {
if (i + 1 >= len)
return false;
let a = fn(data[i + 1]);
if (a.startsWith("10"))
i += 1;
else
return false;
} else if (str.startsWith("1110")) {
if (i + 2 >= len)
return false;
let a = fn(data[i + 1]);
let b = fn(data[i + 2]);
if (a.startsWith("10") && b.startsWith("10"))
i += 2;
else
return false;
} else if (str.startsWith("11110")) {
if (i + 3 >= len)
return false;
let a = fn(data[i + 1]);
let b = fn(data[i + 2]);
let c = fn(data[i + 3]);
if (a.startsWith("10") && b.startsWith("10") && c.startsWith("10"))
i += 3;
else
return false;
} else
return false;
}
return true;
};