题目描述
Determine whether an integer is a palindrome. Do this without extra space.
代码
public static boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
if (x >= 0 && x < 10) {
return true;
}
int d = 1;
while (x / d >= 10) {
d *= 10;
}
while (x != 0) {
// 若首尾不相等,返回false
if (x % 10 != x / d) {
return false;
}
x = x % d / 10;
d /= 100;
}
return true;
}