forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0029.py
31 lines (24 loc) · 767 Bytes
/
0029.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
pos = False if (dividend < 0) ^ (divisor < 0) else True
res, divd, divs = 0, abs(dividend), abs(divisor)
if divd < divs:
return 0
mod = 2**31
while divs <= divd:
mul, tmp = 1, divs
while (tmp << 1) <= divd:
mul <<= 1
tmp <<= 1
res += mul
divd -= tmp
if res == mod:
break
if pos and res == mod:
res -= 1
return res if pos else -res