804 Rotated Digits
804 Rotated Digits
Rotated Digits 
An integer x is a good if after rotating each digit individually by 180 degrees, we get a valid number that is different from x. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
1
2
3
4
0, 1, and 8 rotate to themselves,
2 and 5 rotate to each other (in this case they are rotated in a different direction, in other words, 2 or 5 gets mirrored),
6 and 9 rotate to each other, and
the rest of the numbers do not rotate to any other number and become invalid.
Given an integer n, return *the number of good integers in the range *[1, n].
Example 1:
1
2
3
4
5
6
**Input:** n = 10
**Output:** 4
**Explanation:** There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
Example 2:
1
2
3
4
**Input:** n = 1
**Output:** 0
Example 3:
1
2
3
4
**Input:** n = 2
**Output:** 1
Constraints:
1
1 <= n <= 104
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
impl Solution {
pub fn rotated_digits(n: i32) -> i32 {
/*
for 10 - we have 4
1,0,2,5,6,9
any number made with this combination will be rottaed, may not be valid ans though
if n = 30
all 1 digit +
fix first digit at 1,2 ->
second digit can be 6 combination
so total 12 ? no
number must have a 2,5,6,9 -> 10 in total
so if n = 100
all 1 digit , all 2 digit and no 3 digit
n = 135
0,1 | 1 , 0 ,2 , 6, 9 | 1, 0 , 2, 5, 6,9
*/
let mut combis : Vec<Vec<i32>> = Vec::new();
let valid_arr = [0,1,2,5,6,9, 8];
let mut k = n;
while k > 9 {
let mut remaining = k % 10;
k = k /10 ;
let mut arr = Vec::new();
for m in &valid_arr {
arr.push(*m);
}
combis.push(arr.clone());
}
let mut arr = Vec::new();
for m in &valid_arr {
if k >= *m {
arr.push(*m);
}
}
combis.push(arr.clone());
combis = combis.clone().into_iter().rev().collect();
print!("{:?} {:?}", arr.clone(), combis.clone());
fn recurse(combi : Vec<Vec<i32>>, contains_valid_digits : bool, level : usize, n : i32 , prev : i32) -> i32 {
if level == combi.len()
{
if n >= prev && contains_valid_digits{
return 1;
} else {
return 0;
}
}
let mut res = 0;
let current_level = combi[level].clone();
for m in ¤t_level {
if (*m == 0 || *m == 1 || *m == 8) && !contains_valid_digits {
res += recurse(combi.clone(), false, level + 1, n , prev * 10 + *m);
} else {
res += recurse(combi.clone(), true, level + 1, n, prev * 10 + *m);
}
}
res
}
return recurse(combis.clone(), false, 0, n , 0);
}
}
This post is licensed under CC BY 4.0 by the author.