leetcode练习10:

leetcode练习10:_第1张图片

 

解析:

解法1:转换为字符串

解法2:用数学计算反转


class Solution(object):

    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        xx = x
        if x < 0:
            return False
        reverse = 0
        while x > 0:
            tmp = x%10
            x = x//10
            #先取余后取整(先取整会改变数字)
            reverse = reverse * 10 + tmp
        return reverse == xx

leetcode练习10:_第2张图片

 

你可能感兴趣的:(leetcode学习,leetcode)