LeetCode第九题(Palindrome Number)

Palindrome Number

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

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

Python代码:

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if (x < 0):
            return False
        if (x >= 0 and x < 10):
            return True
        a = 0
        b = x
        while (b > 0):
            a = a * 10 + b % 10
            b = int(b / 10)
        if (a == x):
            return True
        return False

运行结果

猜你喜欢

转载自blog.csdn.net/ax478/article/details/79875174