# 44ms c++ solution

• You can also view more solution on Github

``````class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
int p1 = 0, p2 = x;
while (p2 > 0) {
p1 = p1*10 + p2%10;
p2 /= 10;
}
return p1 == x;
}
};``````

• Great solution..but can you explain me how the overflow condition is handled in your code?

• calculate the reversed number and compare with the origin number.

• The question stats 'do not use extra space'. Your solution does not meet this restriction while using p1 to store the temporary result.

• If the reversed number overflow, the number is not a palindromic! So this code works.

• class Solution {
public:
bool isPalindrome(int x) {
if(x<0|| (x!=0 &&x%10==0)) return false;
int sum=0;
while(x>sum)
{
sum = sum*10+x%10;
x = x/10;
}
return (x==sum)||(x==sum/10);
}
};

why this solution takes 80ms? what is the difference from yours?

• I had the same question. Not sure why it's a longer time.

• I also had the same solution, but my run time is 128 ms; and I don't think it could be faster than the most voted answer.

• The same question with me. And I test this solution just now, which shows 76ms.
And I test another solution just the same as this one but some small difference in the final block that are if(a==num) return true; else return false; This one runs for 88ms...

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.