思路:整数的回文注意保留原来的数,否则,中间有零的时候不好处理。右侧的数字每次模10后就可以,保持数字除以10 ,左侧的数字提取需要更新除数的位数。
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)return false;
int temp = x;
int len = 0;
while(x){
x /= 10;
len++;
}
x = temp;
while(len >= 2){
temp = x / pow(10,len-1);
temp %= 10;
if(x % 10 != temp)return false;
x = x / 10;
len-=2;
}
return true;
}
};