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:
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 ofgood integers in the range _[1, n].
Input: n =10Output: 4Explanation: 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.
A number is good if it contains at least one digit that changes (2,5,6,9) and all digits are valid after rotation (0,1,2,5,6,8,9). We can check each number up to n, or use DP to speed up.
classSolution {
public:int rotatedDigits(int n) {
int res =0;
for (int i =1; i <= n; ++i) {
int x = i, good =0, valid =1;
while (x) {
int d = x %10;
if (d ==3|| d ==4|| d ==7) { valid =0; break; }
if (d ==2|| d ==5|| d ==6|| d ==9) good =1;
x /=10;
}
if (valid && good) ++res;
}
return res;
}
};
classSolution {
publicintrotatedDigits(int n) {
int res = 0;
for (int i = 1; i <= n; ++i) {
int x = i;
boolean valid =true, good =false;
while (x > 0) {
int d = x % 10;
if (d == 3 || d == 4 || d == 7) { valid =false; break; }
if (d == 2 || d == 5 || d == 6 || d == 9) good =true;
x /= 10;
}
if (valid && good) res++;
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funrotatedDigits(n: Int): Int {
var res = 0for (i in1..n) {
var x = i
var valid = truevar good = falsewhile (x > 0) {
val d = x % 10if (d ==3|| d ==4|| d ==7) { valid = false; break }
if (d ==2|| d ==5|| d ==6|| d ==9) good = true x /=10 }
if (valid && good) res++ }
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defrotatedDigits(n):
res =0for i in range(1, n+1):
x = i
valid, good =True, Falsewhile x >0:
d = x %10if d in (3,4,7):
valid =Falsebreakif d in (2,5,6,9):
good =True x //=10if valid and good:
res +=1return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pubfnrotated_digits(n: i32) -> i32 {
letmut res =0;
for i in1..=n {
letmut x = i;
letmut valid =true;
letmut good =false;
while x >0 {
let d = x %10;
if d ==3|| d ==4|| d ==7 { valid =false; break; }
if d ==2|| d ==5|| d ==6|| d ==9 { good =true; }
x /=10;
}
if valid && good { res +=1; }
}
res
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionrotatedDigits(n: number):number {
letres=0;
for (leti=1; i<=n; ++i) {
letx=i, valid=true, good=false;
while (x>0) {
constd=x%10;
if (d===3||d===4||d===7) { valid=false; break; }
if (d===2||d===5||d===6||d===9) good=true;
x= Math.floor(x/10);
}
if (valid&&good) res++;
}
returnres;
}