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.
坑爹白痴的我想的思路就是很复杂的,而且还要考虑很多情况,网上很多方法思路好简单!!
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x==INT_MIN) return false;
if(x<0) return false;
bool isP=true;
x=abs(x);
int y=x;
while(y>=10)
{
int tominus=1;
int t=1;
int k;
int zero_nums=0;
for(k=y;k>=10;k/=10,t++)
{
if(k%10==0) zero_nums++;
tominus*=10;
}
if(y%10 == k)
{
y=(y-k*tominus)/10;
if(y==0) zero_nums=0;
while(y%10==0 && y>0)
{
y=y/10;
zero_nums-=2;
}
}
else
{
return false;
}
if(zero_nums%2!=0)
return false;
}
return isP;
}
};