Leetcode / 9. Palindrome Number
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution {
// time complexity O(log(x))
// space complexity O(1)
public boolean isPalindrome(int x) {
int n = x;
int reversed = 0;
while (n > 0) {
int digit = n % 10;
n /= 10;
reversed = reversed * 10 + digit;
}
return reversed == x;
}
}
Did you like the lesson? 😆👍
Consider a donation to support our work: