Determine whether an integer is a palindrome. Do this without extra space.
bool isPalindrome(int x) {
int d = 0;
int n = x;
if(x<0)return false;
while(n)
{
d++;
n=n/10;
}
int i = d-1, j = 0;
while(i>j)
{
int pi = pow(10,i), pj = pow(10,j), pk = pow(10,j+1);
if(x/pi != (x%pk)/pj)return false;
x = x%pi;
i--;j++;
}
return true;
}
No comments:
Post a Comment