[LeetCode-Easy][Python] 009-Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

方法一

可以参考 007-Reverse Integer,把整数翻转,再判断翻转后的数和原来的数是否相同来判断是否是回文数

1. 不能申请额外空间,所以007-Reverse Integer 方法二中将整数转成字符串,用字符串自带的反转[::-1]的方法就不可用

2. 可以借用007-Reverse Integer 方法一,依旧要注意溢出问题,翻转后不能超过32位 最大值

3. 正负号不用考虑,因为负数不可能是回文数, '-12321' --> '12321-' 是不相等的,所以负数直接返回False

 

def isPalindrome(x):
    a = x
    rev_x = 0 
    if x < 0:
        print('%s is negative' % x)
        return False
    while x > 0:
        rev_x = rev_x * 10 + x % 10
        if rev_x > 2147483647:
            print('x is overflow')
            return False
        x //= 10
    if rev_x == a:
        print('%d is Palindrome' % a)
        return True
    else:
        print('%d is not Palindrome' % a)
        return False

 

 

 

 

 

转载于:https://www.cnblogs.com/FiaFia/p/7903232.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值