问题描述:
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
问题分析:判断一个数是否为回文,很简单。
<span style="font-size:10px;">class Solution {
public:
bool isPalindrome(int x) {
long long xx = x;
long long new_xx = 0;
if( xx < 0) return 0;
else{
while(xx){
new_xx = new_xx * 10 + xx % 10;
xx /= 10;
}
return new_xx == (long long)x;
}
}
};</span>