Palindrome Number – Python 3 (Week 11 – 02)

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        
        ans = 0
        tmp = x
        
        while tmp != 0:
            ans = ans * 10 + tmp % 10
            tmp = tmp // 10
            
        return x == ans
        

Time O(n). Space O(1).

Leave a comment

Design a site like this with WordPress.com
Get started