问题:
Determine whether an integer is a palindrome. Do this without extra space.
问题要求不用额外存储空间判断一个数是否回文,其实可以转化为:
判断将一个数逆序后,是否与原始数字相等。
代码示例:
public class Solution {
public boolean isPalindrome(int x) {
//题目没有约定,提交代码后才知道要求负数全部不是回文的
if(x < 0) {
return false;
}
int temp = x;
long change = 0;
while (x != 0) {
change = change * 10 + x % 10;
if (change > Integer.MAX_VALUE || change < Integer.MIN_VALUE) {
return false;
}
x /= 10;
}
return change == temp;
}
}