"Do it in python" doesn't need overflow check?


  • 0
    B
    class Solution:
    # @return an integer
    def reverse(self, x):
        if x>=0:
            temp=str(x)
            templist=[]
            for char in temp:
                templist.append(char)
            return int("".join(templist[::-1]))
        else :
            temp=str(-x)
            templist=[]
            for char in temp:
                templist.append(char)
            return -int("".join(templist[::-1]))
    

    Doesn't seem there is any need for the overflow check. Am I right? (guess str(int) will just work..)


  • 1
    G

    I think so, though the solution can be more compact like this:

    return [-int(str(abs(x))[::-1]), int(str(abs(x))[::-1])][x>0]

Log in to reply
 

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