class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
res = 0
def isValid(num):
flag = False
while num > 0:
if num%10 == 2 or num%10 == 5 or num%10 == 6 or num%10 == 9:
flag = True
elif num%10 == 3 or num%10 == 4 or num%10 == 7:
return False
num = num/10
return flag
for i in range(1,N+1):
if isValid(i):
res += 1
return res
0,参考了一下
1,题目要求:
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.
也就是说,2,5,6,9是所谓的可翻转的数字,其他的都不是。就像26和95是一对反转数字。
2,一开始也是想到直接按位进行判断,但是想多了,思考了怎么去找翻转后的数字以及是否在给定范围内,其实没有必要的,只要依次进行判断即可,不需要对每一对翻转数进行记录。
因此,对于每一个数字,如果每一位是2或5或6或9,则是可翻转的数字。而只要有一位是3,4,7,则一定不是,直接返回False。而至于像11这样的数字,也是不可翻转的,但是像21这样的数字,翻转数就是15,是可翻转的。因此对于0或者1并不需要专门的判断。