LC-Rotated Digits

406 篇文章 0 订阅
69 篇文章 0 订阅
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并不需要专门的判断。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值