原题链接: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<<shift
ans += 1<<shift
shift -= 1
if neg:
ans = -ans
if ans > Int_MAX:
return Int_MAX
return ans
网友评论