7-line Python solution, bit-manipulation, 48ms, beats 99%


  • 0
    K
    def divide(self, a, b):
        isPositive, a, b, result = (a >= 0) ^ (b < 0), abs(a), abs(b), 0
        while a >= b:
            n, nb = 1, b
            while a >= nb:
                a, result = a - nb, result + n
                n, nb = n << 1, nb << 1
        return min(result, 2147483647) if isPositive else max(-result, -2147483648)

Log in to reply
 

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