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;
}
if (x == 0) {
return true;
}
int base = 1;
int record = x;
while (x >= 10) {
base *= 10;
x /= 10;
}
x = record;
while (x > 0) {
int left = x / base;
int right = x % 10;
if (left != right) {
return false;
}
x -= left * base;
x /= 10;
base /= 100;
}
return true;
}
}