Reverse Integer in Python 3


  • 0
    class Solution:
        def reverse(self, nums):
            if len(str(nums))>0 and nums != 0 and nums <= math.pow(2,31):
                nums = str(nums)
                newStr = ''
                # len(nums)-1 to -1 , -1 not included , interval is -1
                for i in range(len(nums)-1,-1,-1):
                    newStr = newStr + nums[i]
                print(newStr)
                while newStr[0] == '0':
                    newStr= newStr[1:]
                if newStr[-1]=='-':
                    newStr='-'+newStr[:len(newStr)-1]
                if math.fabs(int(newStr)) >= math.pow(2,31):
                    return 0
                else:
                    return(int(newStr))
            else:
                return 0
    

Log in to reply
 

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