Rotated Digits
An integer x is 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:
0,1, and8rotate to themselves.2and5rotate to each other; in this case they are rotated in a different direction, so2or5gets mirrored.6and9rotate to each other.- The rest of the digits do not rotate to any other digit and become invalid.
Given an integer n, return the number of good integers in the range [1, n].
Example 1
Input
n = 10Output
4There are four good numbers in the range [1, 10]: 2, 5, 6, and 9; 1 and 10 are not good because they remain unchanged after rotating.
Example 2
Input
n = 1Output
0The only number in the range [1, 1] is 1, which remains unchanged after rotating, so there are no good numbers.
Constraints
- 1 <= n <= 10^4