We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3d33c4f commit 9bbf1e2Copy full SHA for 9bbf1e2
solution/0393.UTF-8 Validation/Solution.cpp
@@ -0,0 +1,33 @@
1
+class Solution {
2
+public:
3
+ bool validUtf8(vector<int>& data) {
4
+ const unsigned modeContinue = 0xc0 ;
5
+
6
+ int conti = 0 ;
7
+ for (auto it = data.begin(); it < data.end(); ++it)
8
+ {
9
10
+ if (0 == conti) // 首字节
11
12
+ if (*it < 0x80)
13
+ continue ;
14
+ else if (*it < 0xe0)
15
+ conti = 1 ;
16
+ else if (*it < 0xf0)
17
+ conti = 2 ;
18
+ else if (*it < 0xf8)
19
+ conti = 3 ;
20
+ else
21
+ return false ;
22
+ }
23
+ else // 后续字节
24
25
+ --conti ;
26
+ if ((*it & modeContinue) != 0x80)
27
28
29
30
31
+ return 0 == conti ;
32
33
+};
0 commit comments