力扣9.回文数
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
emmm三行解决,用c++的stl
class Solution {
public:
bool isPalindrome(int x) {
string t = to_string(x);
reverse(t.begin(), t.end());
return t == to_string(x);
}
};
法二:
利用第七题的整数反转,一模一样的代码,再加入溢出判断
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
return false;
int num = x, cur = 0;
while (x) {
if (cur > (INT_MAX-x%10)/10)
return false;
cur = cur * 10 + x % 10;
x /= 10;
}
return cur == num;
}
};