题目如下:
解题思路:
将该数反过来排一遍如果与之前的数相同即为回文数,注意 负数 与 0 可直接处理。
代码如下:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) //负数不为回文数
return false;
else if(x == 0) //0为回文数
return true;
long a = 0; //转换时可能存在数组溢出的情况,所以需要长整型
int temp = x;
while(temp){
a = a*10 + temp%10;
temp = temp / 10;
}
if(a == x)
return true;
else
return false;
}
};