Accepted Python code, how to make it better?


  • 0
    J
    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if 0 <= x <= 9:
                return True
            if x > 10 and x % 10 != 0:
                rev = 0
                while x > 0:
                    rev = rev*10 + x % 10
                    x //= 10
                    see = rev*10 + x % 10
                    if x == rev:
                        return True
                    elif see == x:
                        return True
            return False
    

    This is my python code, is my idea OK? How to make it better and more professional?
    Thank you for your advice!


  • 0
    G

    In my solution if you have a palindrome number then check if there equal after revesing it. You could also make a string and then reverse it like so

    Return str(x) == str(x)[::-1]


  • 0
    J

    Thank you so much, this is a smart way to compare the numbers!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.