跳至主要内容

9. Palindrome Number

class Solution {
public:
bool isPalindrome(int x)
{
if (x < 0) return false;
if (x != 0 && x % 10 == 0) return false;
int reverted = 0;
while (x > reverted)
{
int digit = x % 10;
x /= 10;
reverted = reverted * 10 + digit;
}
return x == reverted || x == reverted / 10;
}
};
  • T: O(N)O(N)
  • S: O(1)O(1)