leetcode中Palindrome Number问题

##问题描述

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.

该问题即对回文数字进行判断,上面提示中要求不能开辟新的空间,所以不能将其转换为字符串
进行字符串的回文判断,并且负数也不是回文数字

##JAVA代码
/
代码很巧妙,主要是判断前一半和后一半是否相同
/
public class Solution {
public boolean isPalindrome(int x) {
//假设输入为1231则,rev为13
int rev = 0;
if(x == 0) return true;
if(x < 0 || x % 10 == 0) return false;
while ( x > rev ){
rev = rev * 10 + x % 10;
x = x / 10;
}
return (x == rev || x == rev / 10);
}
}