Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
如果是负数,则不是回文数;如果是零,是回文数,那大于零怎么判断呢?
我思考,如果我们可以把数正着写和逆着写的数字是一样,那这个数就是回文数。
下面见代码。
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
else if(x == 0)
return true;
else
{
int z = x;
int y = 0;
while(x != 0)
{
y = y*10 +x%10;
x = x/10;
}
if(y == z)
return true;
else
return false;
}
}
};