题目:
Determine whether an integer is a palindrome. Do this without extra space.
链接:Palindrome Number
解法:循环,与itoa类似,时间O(n)
class Solution {
public:
bool isPalindrome(int x) {
int ans = 0, y = x;
if (x < 0) {
return false;
}
while (x > 0) {
if (abs(ans) > INT_MAX / 10) return false;
ans = (x % 10) + ans * 10;
x = x / 10;
}
return y == ans;
}
};
Runtime: 156 ms