1
class Solution {
2
public:
3
bool validUtf8(vector<int> &data) {
4
int n = data.size();
5
int count = 0;
6
for (int i = 0; i < n; i++) {
7
int ele = data[i];
8
if (!count) {
9
// if the first 3 bits are 110, then the next byte is part of the
10
// current UTF-8 character
11
if ((ele >> 5) == 0b110) count = 1;
12
// if the first 4 bits are 1110, then the next 2 bytes are part of the
13
// current UTF-8 character
14
else if ((ele >> 4) == 0b1110)
15
count = 2;
16
// if the first 5 bits are 11110, then the next 3 bytes are part of the
17
// current UTF-8 character
18
else if ((ele >> 3) == 0b11110)
19
count = 3;
20
// if the first bit is 1, then return false
21
else if ((ele >> 7))
22
return false;
23
} else {
24
// if the first 2 bits are not 10, then return false
25
if ((ele >> 6) != 0b10) return false;
26
count--;
27
}
28
}
29
return (count == 0);
30
}
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0