My concise solution in Python, around 70 ms


  • 0
    B
    class Solution:
        # @return an integer
        def reverse(self, x):
            ret, i, s = 0, abs(x), -1 if x < 0 else 1
            while i:
                i, ret = i//10, ret * 10+ i%10
            return 0 if ret > pow(2, 31) else ret * s

  • 0
    S
    class Solution:
        # @return an integer
        def reverse(self, x):
            if x >= 0:
                y = int(str(x)[::-1])
                if y > 2147483647:
                    return 0
                return y
            if x < 0 :
                y = int(str(x)[:0:-1]) * -1
                if y < -2147483648:
                    return 0
                return y  

  • 0
    S
    class Solution:
        # @return an integer
        def reverse(self, x):
            sign = 1 if x > 0 else -1
            num = int(str(abs(x))[::-1])*sign
            return 0 if num > 2147483647 or num < -2147483648 else num

Log in to reply
 

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