LeetCode 9. Palindrome Number
Solution1:我的答案
思路一:转化为字符串
class Solution {
public:
bool isPalindrome(int x) {
string x_str = to_string(x);
int n = x_str.size(), left = 0, right = x_str.size() - 1;
while (left < right) {
if (x_str[left] != x_str[right])
return false;
else {
left++;
right--;
}
}
return true;
}
};
思路二:求其逆数比是否相等
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
int res = 0, x_copy = x;
while (x_copy) {
res = res * 10 + x_copy % 10;
x_copy /= 10;
}
if (res != x)
return false;
else
return true;
}
};