题目:
Determine whether an integer is a palindrome. Do this without extra space.
思路:
翻转数字并与原数字比较。
代码:
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x < 0)
{
return false;
}
else if(x == 0)
{
return true;
}
if(x == reverse(x))
{
return true;
}
else
{
return false;
}
}
int reverse(int x)
{
int res = 0;
while(x > 0)
{
res = res * 10 + x %10;
x /= 10;
}
return res;
}
};