rustgym/leetcode/
_1056_confusing_number.rs

1struct Solution;
2
3impl Solution {
4    fn rotate(d: i32) -> Option<i32> {
5        match d {
6            0 => Some(0),
7            1 => Some(1),
8            6 => Some(9),
9            8 => Some(8),
10            9 => Some(6),
11            _ => None,
12        }
13    }
14
15    fn confusing_number(mut n: i32) -> bool {
16        let num = n;
17        let mut rev = 0;
18        while n > 0 {
19            let d = n % 10;
20            if let Some(t) = Self::rotate(d) {
21                rev *= 10;
22                rev += t;
23                n /= 10;
24            } else {
25                return false;
26            }
27        }
28        rev != num
29    }
30}
31
32#[test]
33fn test() {
34    assert_eq!(Solution::confusing_number(6), true);
35    assert_eq!(Solution::confusing_number(89), true);
36    assert_eq!(Solution::confusing_number(11), false);
37    assert_eq!(Solution::confusing_number(25), false);
38}