# An easy c++ 8 lines code (only reversing till half and then compare)

• ``````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);
}
};``````

• Brilliant solution!

• OMG! This solution is so Brilliant! thank you~

• awesome! I reversed the whole units, but you just use half of them!

• it is so amazing!!!

• is this "without extra space" ?

• Brilliant!!!

• So amazing!Thank you man.

• And where 'without extra space' is implemented here?

• Well, "without extra space" typically means `O(1)` additional space instead of not using any additioanl sapce.

• Clever idea!!!

• but why say "If you are thinking of converting the integer to string, note the restriction of using extra space." (maybe I misunderstood

• ``````class Solution {
public:
bool isPalindrome(int x) {
int i=x, n=0;
while(i>0)
{
n=i%10+n*10;
i/=10;
}
return n==x;
}
};
``````

This is my solution, but I don't understand the demand "without extra space"...

• This is 0(1) so without extra space.
You could use variable in (without extra space) as variable come under order(1) domain.

• You could use variables in (without extra space) as variable come under order(1) domain.

• ``````This is my code not being involved in extra space and math algorithm, but looks stupid!
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
strN = str(x)
n = len(strN)
i = 0
j = n - 1
while(i <= j):
if(strN.__getitem__(i) != strN.__getitem__(j)):
return False
i += 1
j -= 1

return True
``````

• what a brilliant solution! so it is O(lgn), right?

• Without extra space, it has to cost O(n). If you want O(logn), extra O(n) space is necessary.

• Thanks for sharing this fabulous one!

• No, the space complexity of “strN" is O(n)

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