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

本文介绍了一种不使用额外空间判断整数是否为回文数的方法,并提供了一个Python实现示例。讨论了负数是否可以作为回文数,以及如何避免整数反转时可能发生的溢出问题。
8万+

被折叠的 条评论
为什么被折叠?



