My Python solution


  • 0
    B

    I wonder if this solution is any slower than https://discuss.leetcode.com/topic/8714/clear-python-code
    but its more simple in logic

    class Solution(object):
        def divide(self, dividend, divisor):
            """
            :type dividend: int
            :type divisor: int
            :rtype: int
            """
            pos = (dividend >= 0) is (divisor >= 0)
            dividend, divisor = abs(dividend), abs(divisor)
            res = 0
            while dividend >= divisor:
                i = 0
                while dividend >= divisor << i:
                    i+=1
                res += 1<<i-1
                dividend -= divisor << i-1
            if not pos:
                res = -res
            return min(max(-2147483648, res), 2147483647)
    

Log in to reply
 

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