回文数:link
1.题目分析
1.首先按照题意,负数是不可能是回文数的。
2.反转一个数字可能会出现的是溢出,在本题中有三种方法处理溢出;
法一:将数字转化为字符串tostr()
函数。
法二:使用long long
类型作为转化后的数可以避免溢出。
法三:只转化数的一半,reference
2.示例代码
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
long long reverse_num = 0;;
int copy_x = x;
while(copy_x){
reverse_num = reverse_num * 10 + copy_x % 10;
copy_x /= 10;
}
return reverse_num == x;
}
};