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

Commit 2aa06f8

Browse files
add utf_8_validation (#29)
1 parent 3c096a2 commit 2aa06f8

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ cargo test
6666
0318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/maximum-product-of-word-lengths.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py) <br>------</br> [Rust](./kamyu104/src/maximum_product_of_word_lengths.rs) | _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium || Bit Manipulation, Counting Sort, Pruning|
6767
0342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/power-of-four.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/power-of-four.py) <br>------</br> [Rust](./kamyu104/src/power_of_four.rs) | _O(1)_ | _O(1)_ | Easy | |
6868
0371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/sum-of-two-integers.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/sum-of-two-integers.py) <br>------</br> [Rust](./kamyu104/src/sum_of_two_integers.rs) | _O(1)_ | _O(1)_ | Easy | LintCode |
69-
0389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/find-the-difference.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-the-difference.py) <br>------</br> [Rust](./kamyu104/src/find_the_difference.rs) | _O(n)_ | _O(1)_ | Easy | |
69+
0389 | [~~Find the Difference~~](https://leetcode.com/problems/find-the-difference/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/find-the-difference.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-the-difference.py) <br>------</br> [Rust](./kamyu104/src/find_the_difference.rs) | _O(n)_ | _O(1)_ | Easy | |
7070
0393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/utf-8-validation.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/utf-8-validation.py) <br>------</br> [Rust](./kamyu104/src/utf_8_validation.rs) | _O(n)_ | _O(1)_ | Medium | |
7171

7272
<!--

kamyu104/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod bitwise_and_of_numbers_range;
2+
mod find_the_difference;
23
mod maximum_product_of_word_lengths;
34
mod missing_number;
45
mod number_of_1_bits;
@@ -9,6 +10,5 @@ mod single_number;
910
mod single_number_ii;
1011
mod single_number_iii;
1112
mod sum_of_two_integers;
12-
mod find_the_difference;
13-
mod utf_8_validation;
1413
mod template;
14+
mod utf_8_validation;

kamyu104/src/utf_8_validation.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
pub struct Solution {}
4+
impl Solution {
5+
pub fn valid_utf8(data: Vec<i32>) -> bool {
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
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn test_valid_utf8() {
38+
assert_eq!(Solution::valid_utf8(vec![197, 130, 1]), true);
39+
assert_eq!(Solution::valid_utf8(vec![235, 140, 4]), false);
40+
}
41+
}

0 commit comments

Comments
 (0)