leetcode easy中的水题一个,负数不是回文,而且不用考虑把数逆转后是否溢出的情况也能AC,逆转后溢出两个数应该不会相等
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
int tmp = x, pali = 0;
while(tmp){
pali = pali*10 + tmp%10;
tmp /= 10;
}
if(pali == x)
return true;
else
return false;
}
};
即使溢出也可用用字符串存储解决
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
string s1,s2;
while(x){
s1.push_back(x%10 + '0');
x /= 10;
}
for(int i = s1.size()-1; i >= 0; i--){
s2 += s1[i];
}
if(s1 == s2)
return true;
else
return false;
}
};