法1:转换成字符串
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return 0;
string s = to_string(x);
return s == string(s.rbegin(), s.rend());
}
};
法2:
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return 0;
int y = x;
long long r = 0; // 使用int 会爆int
while (x) {
r = r * 10 + x % 10;
x /= 10;
}
return r == y;
}
};