-
Notifications
You must be signed in to change notification settings - Fork 0
/
393.cpp
70 lines (69 loc) · 2.04 KB
/
393.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
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
bool validUtf8(vector<int>& data) {
bool result=true;
int i=0;
int limit_size=data.size();
while(i<limit_size){
if(data[i]>=248 or(data[i]>=128 and data[i]<192)){
result=false;
break;
}
if((data[i]>>7)==0){
if(i+1>=limit_size){
result=true;
}
i++;
}else{
int check=data[i]>>4;
if(check==0b1100 or check==0b1101){
if(i+1>=limit_size){
result=false;
break;
}
if(data[i+1]>>6!=0b10){
result=false;
break;
}
i+=2;
}else if(check==0b1110){
if(i+2>=limit_size){
result=false;
break;
}
for(int j=1;j<=2;j++){
if(data[i+j]>>6!=0b10){
result=false;
break;
}
}
if(result==false){break;}
i+=3;
}else if(data[i]>>3==0b11110){
if(i+3>=limit_size){
result=false;
break;
}
for(int j=1;j<=3;j++){
if(data[i+j]>>6!=0b10){
result=false;
break;
}
}
if(result==false){break;}
i+=4;
}
}
}
return result;
}
};
int main(){
Solution a;
vector<int>b={240,162,138,147};
cout<<a.validUtf8(b)<<endl;
return 0;
}