-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivide_Two_Integers.py
More file actions
38 lines (29 loc) · 1.12 KB
/
Divide_Two_Integers.py
File metadata and controls
38 lines (29 loc) · 1.12 KB
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
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
# Constants for the problem constraints
INT_MAX = 2**31 - 1
INT_MIN = -2**31
# Special case: overflow
if dividend == INT_MIN and divisor == -1:
return INT_MAX
# Compute the sign of the result
sign = 1 if (dividend > 0) == (divisor > 0) else -1
# Take the absolute values
dividend, divisor = abs(dividend), abs(divisor)
# Result starts from 0
quotient = 0
# While the dividend is greater or equal to the divisor
while dividend >= divisor:
# Start from a divisor and a multiple of 1
current_divisor, multiple = divisor, 1
# While the dividend is greater or equal to the current divisor
while dividend >= current_divisor:
# Subtract the current divisor from the dividend
dividend -= current_divisor
# Add the current multiple to the quotient
quotient += multiple
# Double the current divisor and its multiple
current_divisor <<= 1
multiple <<= 1
# Return the final quotient, with the correct sign
return sign * quotient