1. Question
确定一个数是否是回文数。要求不使用额外空间。
Determine whether an integer is a palindrome. Do this without extra space.
2. Solution
如果是负数,就不是回文数。
2.1 reverse integer
采用反转整数的方法,注意处理溢出(采用long存储数据)
public class Solution { public boolean isPalindrome(int x) { if( x < 0 ) return false; long rev = 0; int y = x; while( y != 0 ){ rev = rev * 10 + y % 10; y = y / 10; } if( rev == (long)x ) return true; return false; } }