剑指 Offer II 001. 整数除法

image.png

原题链接:https://leetcode-cn.com/problems/xoh6Oh/

解题思路:

Python代码

class Solution:
    def divide(self, a: int, b: int) -> int:
        Int_MAX = 2**31 - 1
        if b == 0:
            return Int_MAX

        neg = a > 0 and b < 0 or a < 0 and b > 0
        a, b = abs(a), abs(b)
        ans, shift = 0, 31
        while shift >= 0:
            if a >= b << shift:
                a -= b< Int_MAX:
            return Int_MAX

        return ans
        

你可能感兴趣的:(剑指 Offer II 001. 整数除法)