题目:
题解:
class Solution:def divide(self, dividend: int, divisor: int) -> int:INT_MIN, INT_MAX = -2**31, 2**31 - 1# 考虑被除数为最小值的情况if dividend == INT_MIN:if divisor == 1:return INT_MINif divisor == -1:return INT_MAX# 考虑除数为最小值的情况if divisor == INT_MIN:return 1 if dividend == INT_MIN else 0# 考虑被除数为 0 的情况if dividend == 0:return 0# 一般情况,使用类二分查找# 将所有的正数取相反数,这样就只需要考虑一种情况rev = Falseif dividend > 0:dividend = -dividendrev = not revif divisor > 0:divisor = -divisorrev = not revcandidates = [divisor]# 注意溢出while candidates[-1] >= dividend - candidates[-1]:candidates.append(candidates[-1] + candidates[-1])ans = 0for i in range(len(candidates) - 1, -1, -1):if candidates[i] >= dividend:ans += (1 << i)dividend -= candidates[i]return -ans if rev else ans