Description:
Determine whether an integer is a palindrome. Do this without extra space.
分析:确定数字是否为回文数。将数字倒置,与原数一致即是回文数;否则,则不是。
代码:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int ret = 0;
int tmp = x;
while(tmp != 0)
{
if(abs(ret) > INT_MAX / 10)
return false;
ret = ret * 10 + tmp % 10;
tmp /= 10;
}
return x == ret;
}
};
【你必须非常努力 才能看起来毫不费力】