palindrome number

来源:互联网 发布:java给app提供接口 编辑:程序博客网 时间:2024/05/29 02:33

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

click to show spoilers.

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.

https://leetcode.com/discuss/23563/line-accepted-java-code-without-the-need-handling-overflow

第二个方法很好,当溢出的时候,值为负,直接返回false.第一个方法,比较一半的数,不用考虑溢出,

return (x==rev || x==rev/10);考虑的是x为偶数位或奇数位两种情况。




0 0