Palindrome Number

Problem Id: 9 Difficulty: Easy Tag: Math


Intuition

The Follow up of this problem is to not convert the integer to string. So we can reverse the integer and compare if it is the same.

Solution


class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False

        reverse = 0
        origin = x
        while x:
            reverse = reverse * 10 + x % 10
            x //= 10

        return reverse == origin