# My C++ solution without extra variables (with comments)

• ``````class Solution {
public:
bool isPalindrome(int x) {
// Negative numbers are not palindromes
if (x < 0) return false;

while (x) {
// If there is only one digit than it is a palindrome
if (x / 10 == 0) return true;

// Compare head and tail
if (x / pow10(size(x)) != x % 10) return false;

// There should be the same number of zeros on the right (ignoring the tail)
// The + 1 is to account for the tail, then we ignore it (dividing by 10)
// If this value is greater than 0 it means that there is some non-zero digit
if ((x % pow10(zerosAfterHead(x) + 1)) / 10 > 0) return false;

x = x / pow10(zerosAfterHead(x) + 1); // Remove tail and zeros before it
x = x % pow10(size(x)); // Remove head (and zeros after it)
}
return true;
}

private:
int size(int n) {
// Number of digits in n
return (int) floor(log10(n));
}
int zerosAfterHead(int n) {
// If the next digit is non-zero the difference will be equal to 1
return size(n) - size(n % pow10(size(n))) - 1;
}
int pow10(int p) {
return (int) pow(10, p);
}
};``````

• mine is quite similar with yours.

• ``````class Solution { // this solution converts int into char. Although it's of low efficiency, it is quite interesting.
public: // i guess this is the way we humans think LOL.
bool isPalindrome(int x)
{
if (x < 0)
return false;

int m;
m = DigitsOfNumber(x);
char * int2char;
int2char = ConvertInt2Char(m, x);
int head = 0;
int tail = m-1;
{
{
if(head == tail || head == tail - 1)
{
return true;
}
tail--;
}
else
{
return false;
break;
}
}
delete [] int2char;
}

private:
int DigitsOfNumber(int x)
{
int m = 1; // m is digits of n
while (x/10 != 0)
{
x = x/10;
m++;
}
return m;
}

char * ConvertInt2Char(int m, int x)
{
char * int2char = new char[m];
for (int i=0;i<=m-1;i++)
{
int2char[i] = '0' + x/pow(10,m-i-1);
x = x%(int)pow(10,m-i-1);
cout << int2char[i] << endl;
}
return int2char;
}
};``````

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