问题:链接
Determine whether an integer is a palindrome. Do this without extra space.
解答:
不能用额外的元素,但是几个变量是可以用的。
如果是负数,直接返回false
否则 见代码。
代码:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
{
return false;
}
int t, w;
t = x, w = 0;
while(t)
{
w = w*10 + t%10;
t /= 10;
}
if(w == x)
return true;
else
return false;
}
};