-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path029 Divide Two Integers.py
38 lines (33 loc) · 1017 Bytes
/
029 Divide Two Integers.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
32
33
34
35
36
37
38
'''
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
'''
class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
MAX_INT = 2147483647
sign = 1
if dividend >= 0 and divisor < 0 or dividend <= 0 and divisor > 0:
sign = -1
dividend = abs(dividend)
divisor = abs(divisor)
result = 0
current = divisor
currentResult = 1
while current <= dividend:
current <<= 1
currentResult <<= 1
while divisor <= dividend:
current >>= 1
currentResult >>= 1
if current <= dividend:
dividend -= current
result += currentResult
return min(sign * result, MAX_INT)
if __name__ == "__main__":
assert Solution().divide(5, -1) == -5
assert Solution().divide(10, 2) == 5