Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bd522ea

Browse files
committed
add utf_8_validation
1 parent 946feab commit bd522ea

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

kamyu104/src/utf_8_validation.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
// Time: O(n)
2+
// Space: O(1)
13
pub struct Solution {}
24
impl Solution {
35
pub fn valid_utf8(data: Vec<i32>) -> bool {
4-
true
6+
let mut count: i32 = 0;
7+
for c in data {
8+
match count {
9+
0 => {
10+
if (c >> 5) == 0b110 {
11+
count = 1;
12+
} else if (c >> 4) == 0b1110 {
13+
count = 2;
14+
} else if (c >> 3) == 0b11110 {
15+
count = 3;
16+
} else if c >> 7!=0 {
17+
return false;
18+
}
19+
},
20+
_ => {
21+
if (c >> 6) != 0b10 {
22+
return false;
23+
}
24+
count-=1;
25+
},
26+
}
27+
}
28+
count == 0
529
}
630
}
731

0 commit comments

Comments
 (0)