[leetcode]#9. Palindrome Number

来源:互联网 发布:舔美网络大v 编辑:程序博客网 时间:2024/06/05 00:37
  • 判断一个整数(integer)是否是回文
  • 不允许用额外空间,所以不能将数转换成字符串来判断。
  • 实际上将原数字反转一半就可以判断是否是回文了。另外,以0结尾的非零数都不是回文。
class Solution(object):    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        if x < 0 or (x != 0 and x%10 == 0):            return False        y = 0        while x > y:            y = y*10 + x%10            x = x/10        return x == y or y/10 == x
class Solution:    # @return a boolean    def isPalindrome(self, x):        if x < 0:            return False        div = 1        while x/div >= 10:            div = div * 10        while x:            left = x / div            right = x % 10            if left != right:                return False            x = ( x % div ) / 10            div = div / 100        return True