-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0246_strobogrammatic_number.rs
More file actions
48 lines (44 loc) · 1.23 KB
/
s0246_strobogrammatic_number.rs
File metadata and controls
48 lines (44 loc) · 1.23 KB
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
#![allow(unused)]
pub struct Solution {}
use std::collections::HashSet;
impl Solution {
// O(n) O(n)
pub fn is_strobogrammatic(num: String) -> bool {
let mut map = HashSet::new();
map.insert("0");
map.insert("1");
map.insert("8");
map.insert("00");
map.insert("11");
map.insert("88");
map.insert("69");
map.insert("96");
let chs = num.chars().collect::<Vec<char>>();
let (mut left, mut right) = (0, chs.len() - 1);
while left <= right {
let mut tmp = "".to_owned();
tmp.push(chs[left]);
tmp.push(chs[right]);
if !map.contains(&tmp.as_str()) {
return false;
}
left += 1;
if right == 0 {
break;
}
right -= 1;
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_246() {
assert_eq!(Solution::is_strobogrammatic("69".to_owned()), true);
assert_eq!(Solution::is_strobogrammatic("88".to_owned()), true);
assert_eq!(Solution::is_strobogrammatic("962".to_owned()), false);
assert_eq!(Solution::is_strobogrammatic("1".to_owned()), true);
}
}