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;
}else if(x < 10){
return true;
}
//找到最高位
int step = 10;
while(x / step >= 10){
step *= 10;
}
while(step >= 1){
int high = x / step;
int low = x % 10;
if(high != low){
return false;
}
x -= high * step;
x /= 10;
step /= 100;
}
return true;
}
}