LeetCode 1088. Confusing Number II

Problem Statement

Backtrack.


impl Solution {
fn rotate(xx: i64) ->i64 {
let (mut x, mut y) = (xx, 0);
while x != 0 {
let mut k = x % 10;
if k == 6 {
k = 9;
} else if k == 9 {
k = 6;
}
y = y * 10 + k;
x = x / 10;
}
return y;
}

fn search(num: i64, n: i32, ans: &mut i32) {
if num > n as i64 {
return;
}
if num != 0 {
if num != Self::rotate(num) {
*ans += 1;
}
Self::search(num * 10, n, ans);
}
Self::search(num * 10 + 1, n, ans);
Self::search(num * 10 + 6, n, ans);
Self::search(num * 10 + 8, n, ans);
Self::search(num * 10 + 9, n, ans);
}
pub fn confusing_number_ii(n: i32) -> i32 {
let mut ans = 0;
Self::search(0, n, &mut ans);
return ans;
}
}