My accepted Python solution, how to get faster?

  • 0

    I have the following Python solution, which is accepted. However, I feel that it can be improved, to check for overflow in a faster manner. Am I guessing it right?

    class Solution(object):
        def reverse(self, x):
            :type x: int
            :rtype: int
            y = abs(x)   
            mod, rem = divmod(y, 10)
            result = rem
            while mod:
                mod, rem = divmod(mod, 10)
                result = 10*result + rem
            result = result*((-1)**(x < 0))
            # check for overflow
            if result > 2**31 -1 or result < -2**31:
              return 0
              return result

Log in to reply

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