One line Python solution


  • 2
    R
    class Solution:
        # @param {integer} x
        # @return {boolean}
        def isPalindrome(self, x):
            return str(x)==str(x)[::-1]

  • 0
    J

    Although it is correct, I think It is not supposed to use extra space in this question


  • 0
    L

    extra space?


  • 0
    S

    You mean when change int x to str x, it cost extra space? I also confused about this part.


  • 0
    J
    This post is deleted!

  • 0
    J

    I believe [::-1] operation will first create a shallow copy of the original char array and then reverse the elements in it.


  • 0
    S

    shallow copy just create a reference of original object. Does it cost extra space? And do you have better solution? Or can we use other variable with constant space to solve it? Thank you!


  • 0
    J

    When creating a shallow copy, "A new object is created that has an exact copy of the values in the original object." It does need extra space to allocate it in this sense. This does work amazingly but it is simply a syntactic sugar from Python. The real solution the recruiters are looking for should be done in O(n) with no extra space I think.


  • 0
    S

    Thanks, did you post your solution on the discuss forum? I can't figure it out...


Log in to reply
 

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