#788
Rotated Digits
specialist · 835 · lc medium +31 · verified · 56.8% accepted · 785 likes · top 52%
Description
An integer x is "good" if rotating every one of its digits by 180° produces a different but still valid integer. Rotating 0, 1, or 8 yields themselves; 2 and 5 swap; 6 and 9 swap; digits 3, 4, and 7 become invalid after rotation.
Given an integer n, count and return the number of good integers in [1, n].
Example 1:
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:
Input: n = 1
Output: 0
Example 3:
Input: n = 2
Output: 1
Code
1
2
3