-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
28 lines (28 loc) · 770 Bytes
/
Copy pathmain.js
File metadata and controls
28 lines (28 loc) · 770 Bytes
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
/**
* @param {number[]} data
* @return {boolean}
*/
var validUtf8 = function(data) {
function helper (index, data) {
if (index === data.length) return true
const byte = data[index]
if (byte[0] === '0') return helper(index + 1, data)
let ones = byte.indexOf('0')
if (ones === -1) ones = 8
if (ones === 1 || ones > 4) return false
for (let i = index + 1; i < index + ones; i++) {
if (i >= data.length) return false
if (data[i][0] !== '1' || data[i][1] !== '0') return false
}
return helper(index + ones, data)
}
data = data.map((item) => {
const arr = []
for (let i = 0; i < 8; i ++) {
arr.push(item % 2)
item = item >> 1
}
return arr.reverse().join('')
})
return helper(0, data)
};