Solution in Python 3 executing in 56ms


  • 0
    K

    class Solution:

    def reverse(self, x):
        numstr = str(x)
        newstr = ''
        length = len(numstr)
        isneg = False
        
        for i in range(0,length):
            if numstr[i] == "-":
                isneg = True
                continue
            newstr = numstr[i] + newstr
        newstr = int(newstr)
        if isneg:
            newstr = 0 - newstr     
            
        if(abs(newstr) > (2 ** 31)-1):
            return 0
        return newstr

Log in to reply
 

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