Reverse Integer solution in python


  • 0
    T

    class Solution(object):
    def reverse(self, x):
    isNegative = x < 0
    ret = 0
    x = abs(x)
    while x > 0:
    ret *= 10
    ret += x % 10
    x /= 10
    if ret > 1<<31:
    return 0

    if isNegative:
        return 0 - ret
    else:
        return ret

  • 0
    W

    class Solution:
    def reverse(self, x):
    if x>231-1:
    return 0
    if x>0 and int(str(x)[::-1])<2
    31-1:
    return int(str(x)[::-1])
    if x==0:
    return 0
    if x<0 and int(str(abs(x))[::-1])<2*31-1:
    return int(str(abs(x))[::-1])
    -1
    else:
    return 0


Log in to reply
 

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