题目:
Determine whether an integer is a palindrome. Do this without extra space.
代码:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int sum = 0;
if(x == 0) return true;
if(x % 10 == 0) return false;
while(x > sum) {
sum = sum * 10 + x % 10;
x /= 10;
}
return x == sum ||x == sum / 10;
}
};
注意点:
x % 10 == 0的时候特判