LeetCode 29. [Python] Divide Two Integers
原题链接
中等
作者:
徐辰潇
,
2020-03-31 00:13:03
,
所有人可见
,
阅读 769
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
#TC: O(log dividend)
#SC: O(log dividend)
sign = -1
if dividend >= 0 and divisor > 0 or dividend <= 0 and divisor < 0:
sign = 1
dividend = abs(dividend)
divisor = abs(divisor)
res = 0
if dividend < divisor:
return res
tmp = divisor
quot = 1
while(tmp + tmp <= dividend):
tmp = tmp + tmp
quot = quot + quot
res_tmp = self.divide(dividend - tmp, divisor)
res = res_tmp + quot
if sign > 0:
return min(res, 2**31 - 1)
else:
return 0 - res