-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
59 lines (50 loc) · 1.05 KB
/
main.cpp
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
57
58
59
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
bool validUtf8(vector<int>& data) {
int carry = 0;
for (int d : data) {
if (carry == 0) {
if ((d >> 3) == 0b11110) {
carry = 3;
} else if ((d >> 4) == 0b1110) {
carry = 2;
} else if ((d >> 5) == 0b110) {
carry = 1;
} else if ((d >> 7) == 0b0) {
carry = 0;
} else {
return false;
}
} else {
if ((d >> 6) != 0b10) {
return false;
}
carry--;
}
}
return carry == 0;
}
};
int main() {
Solution sol;
vector<int> data;
data = {197,130,1};
cout << sol.validUtf8(data) << endl;
data = {235,140,4};
cout << sol.validUtf8(data) << endl;
data = {235,140,130,4};
cout << sol.validUtf8(data) << endl;
data = {140,130,4};
cout << sol.validUtf8(data) << endl;
return 0;
}