788. Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X.
A number is valid if each digit remains a digit after rotation. 0, 1, 8
rotate to themselves; 2, 5
rotate to each other; 6, 9
rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
Now given a positive number N, how many numbers X from 1 to N are good?
Input: 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.
判断数字旋转180°后为非本身数字的个数
方法1:5ms
构建N+1的数组,对应存储每个数字的标记
2569对应标记words[i]=2,018对应标记words[i]=1
当i < 10时,i的标记值可直接确定
当i >= 10时,i的标记值直接由 i/10
和 i%10
决定即可。由于i是从小到大依次运行的,所以可直接读取之前已经确定好的words[i / 10]和words[i % 10]。
public int rotatedDigits2(int N) {
int[] words = new int[N + 1];
int count = 0;
for (int i = 0; i <= N; i++) {
if (i < 10){
if (i ==</