-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-bit-and-2-bit-characters.rs
57 lines (50 loc) · 1.36 KB
/
one-bit-and-2-bit-characters.rs
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
struct Solution;
impl Solution {
fn impl1(bits: Vec<i32>) -> bool {
let mut bit = 0;
let mut ans = true;
for i in bits {
if i == 1 || bit == 1 {
bit = if bit + 1 == 2 { 0 } else { 1 };
ans = false;
} else {
ans = true;
}
}
ans
}
#[allow(unused)]
fn impl2(bits: Vec<i32>) -> bool {
let mut s = 0;
while s < bits.len() - 1 {
s = if bits[s] == 0 { s + 1 } else { s + 2 };
}
s == bits.len() - 1
}
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
Solution::impl1(bits)
}
}
fn main() {
println!("{}", Solution::is_one_bit_character(vec![1, 0, 0]));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test_impl1() {
assert!(Solution::impl1(vec![1, 0, 0]));
assert!(!Solution::impl1(vec![1, 1, 1, 0]));
assert!(Solution::impl1(vec![1, 1, 0, 0]));
assert!(Solution::impl1(vec![0, 1, 0, 0]));
assert!(Solution::impl1(vec![0]));
}
#[test]
fn test_impl2() {
assert!(Solution::impl2(vec![1, 0, 0]));
assert!(!Solution::impl2(vec![1, 1, 1, 0]));
assert!(Solution::impl2(vec![1, 1, 0, 0]));
assert!(Solution::impl2(vec![0, 1, 0, 0]));
assert!(Solution::impl2(vec![0]));
}
}