LeetCode Solutions
9. Palindrome Number
Time: $O(\log x)$ Space: $O(\log x)$
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
return false;
long reversed = 0;
int y = x;
while (y) {
reversed = reversed * 10 + y % 10;
y /= 10;
}
return reversed == x;
}
};
class Solution {
public boolean isPalindrome(int x) {
if (x < 0)
return false;
long reversed = 0;
int y = x;
while (y > 0) {
reversed = reversed * 10 + y % 10;
y /= 10;
}
return reversed == x;
}
}
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
rev = 0
y = x
while y:
rev = rev * 10 + y % 10
y //= 10
return rev == x