-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path393.cpp
More file actions
executable file
·34 lines (34 loc) · 1.02 KB
/
393.cpp
File metadata and controls
executable file
·34 lines (34 loc) · 1.02 KB
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
class Solution {
public:
bool validUtf8(vector<int>& data) {
if (data.size() == 1){
if ((data[0] >> 7 & 1) == 1)
return false;
else
return true;
}else{
int index = 0;
while (index < data.size()){
int nums = 0;
int offset = 7;
while (offset >= 0 && (data[index] >> offset & 1) == 1){
nums ++;
offset--;
}
if (nums > 4)
return false;
for (int i = index + 1; i < index + nums; i++){
if (i >= data.size()) return false;
if ((data[i] >> 7 & 1) != 1 || (data[i] >> 6 & 1) != 0) return false;
}
if (nums == 0)
index ++;
if (nums == 1)
return false;
if (nums > 0)
index += nums;
}
}
return true;
}
};