Python solution without extra space


  • -2
    G
    def isPalindrome(self, x):
        if x < 0:
            return False
        copy = x
        count = 0
        while copy >= 10:
            copy /= 10
            count += 1
        copy = x
        while count >= 0:
            left = copy / 10 ** count - (copy / 10 ** (count + 1)) * 10
            count -= 1
    
            if count >= 0:
                right = copy % 10
                copy /= 10
                count -= 1
                if left != right:
                    return False
        return True

  • 0
    K

    doesn't this also require extra space to save the number?


  • 0
    G

    I think I just use several integers, it's constant space, which means no extra space.


  • 0
    G
    This post is deleted!

Log in to reply
 

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