Python 1 line solution


  • 0
    N
    def isPalindrome(x):
        return False if x < 0 else x == int(str(x)[::-1])
    

    Looks neat. That's it :D


  • 0

    "neat"? Nah...

    The I'd say "normal" version:

    def isPalindrome(self, x):
        return x >= 0 and x == int(str(x)[::-1])
    

    And one I do find somewhat neat:

    def isPalindrome(self, x):
        return 0 <= x == int(str(x)[::-1])
    

    Though this is still simpler:

    def isPalindrome(self, x):
        return str(x) == str(x)[::-1]
    

    Btw, yours doesn't work, since it's lacking the self parameter.


  • 0
    A

    @nvanthao You're using extra space by converting x to string.


Log in to reply
 

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