前言
- 文章作为日记或心得,记录学习过程
- 本文记录本题(源自LeetCode)遇到的所有问题、疑惑
- 如对内容有任何建议或看法,欢迎评论区学习交流
正文
题目
解答
该题和上回的整数反转相似,无非最后多了个判断
class Solution {
public:
bool isPalindrome(int x) {
int tran=0;
if(x<0)
return false;
else if(x==0)
return true;
else
{
int x1=x;
while(x1>0)//这里写成while(x1)也可
{
if(tran>INT_MAX/10||tran<INT_MIN/10)//这里仍需注意tran的取值范围,防止溢出
return false;
tran=tran*10+x1%10;
x1=x1/10;
}
if(tran==x)
return true;
else
return false;
}
}
};