-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy paths1.cpp
More file actions
23 lines (23 loc) · 737 Bytes
/
Copy paths1.cpp
File metadata and controls
23 lines (23 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
private:
bool validSuffix(vector<int> &data, int &i, int cnt) {
while (cnt && i < data.size() && (data[i] >> 6) == 2) ++i, --cnt;
return cnt == 0;
}
public:
bool validUtf8(vector<int>& data) {
for (int i = 0; i < data.size(); ) {
int n = data[i++];
if (n >> 7 == 0) {
// noop
} else if (n >> 5 == 6) {
if (!validSuffix(data, i, 1)) return false;
} else if (n >> 4 == 14) {
if (!validSuffix(data, i, 2)) return false;
} else if (n >> 3 == 30) {
if (!validSuffix(data, i, 3)) return false;
} else return false;
}
return true;
}
};