# Two-pointer style solution, walking from left and right to the middle

• I wanted to check if the number is a palindrome the same way you would check if a string is a palindrome, with a left pointer and right pointer that walk toward the middle. The only trick is finding the largest multiple of 10 smaller than x, for which I use the pow, floor, and log10 functions:

``````bool isPalindrome(int x) {
if (x < 0) return false;

int left = pow(10, floor(log10(x)));
int right = 1;
while (right < left) {
if ((x / left - x / right) % 10 != 0) {
return false;
}
right *= 10;
left /= 10;
}

return true;
}``````

• Another way to do this without using pow() and log() functions:

``````bool isPalindrome(int x) {
if (x < 0) return false;
int probe=1;
while (x/probe>=10) probe*=10;
while (probe>1)
{
int left=x/probe;
int right=x%10;
if (left!=right) return false;
x=(x-left*probe)/10;
probe/=100;
}
return true;
}``````

• Hi, I am wondering if x=0, then log10(0) will cause error right? You have passed the test, I think there should be no x = 0 input case. Right?

• You're right. It returns -inf.

You could handle that by checking for 0 or by using a simple loop like the one in LMunique's answer (which is also faster).

• it has 0 input case, i saw it when submit one of my solutions. so why this get passed?

• this is the best solution

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