题目地址
题目描述
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, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number.
Now given a positive number N
, how many numbers X from 1
to N
are good?
1 | Example: |
Note:
- N will be in range
[1, 10000]
.
解题思路
这道题要求找出从1到N中满足数字能够旋转180°且会变成另外一个有意义数字的数字的个数。
1. 能够旋转180°
2. 旋转后与当前数字不相等
我们观察得知,要满足能够旋转180°,那么数字中每一位都满足为可以旋转为另一个数字,即1,2,5,6,8,9,0,要满足旋转后与当前数字不相等则需要满足至少有一位数字属于2,5,6,9中的一个。即满足存在某一位的数字属于2,5,6,9且不能出现3,4,7。
解题代码【.CPP】
1 | class Solution { |