Determine whether an integer is a palindrome. Do this without extra space.
Determine whether an integer is a palindrome. Do this without extra space.
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.
def isPalindrome(x): if x<0 or x>0 and x % 10==0: return False n = x sum = 0 while n: sum = sum*10 + n%10 n = n//10 if sum >= n: break return sum == n or sum//10 == n print isPalindrome(110111)
Determine whether an integer is a palindrome. Do this without extra space.
本文介绍了一种不使用额外空间判断整数是否为回文数的方法。文章提供了一个Python函数实现,该方法考虑了负数、整数反转可能溢出等问题,并通过一个示例展示了如何判断110111是否为回文数。
141

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



