Python , 18 lines of code with O(N) time complexity


  • 0
    R

    class Solution(object):
    def reverse(self, x):
    s=""
    temp=x
    if x<0:
    x=abs(x)
    carryover,remainder = divmod(x,10)
    s += str(remainder)
    while(carryover):
    carryover,remainder = divmod(carryover,10)
    s += str(remainder)
    if -int(s)<-2147483648:
    s="0"
    if temp<0:
    s = -int(s)
    return s
    else:
    return int(s)


Log in to reply
 

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