-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0029M. Divide Two Integers.py
More file actions
60 lines (56 loc) · 2.29 KB
/
0029M. Divide Two Integers.py
File metadata and controls
60 lines (56 loc) · 2.29 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#Runtime: 24 ms, faster than 98.12% of Python3 online submissions for Divide Two Integers.
#Memory Usage: 14.3 MB, less than 20.50% of Python3 online submissions for Divide Two Integers.
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
if dividend == 0:
return 0
if divisor == 1:
return dividend
if divisor == -1:
if dividend == -2147483648:
return 2147483647
return 0 - dividend
if dividend > 0 and divisor > 0:
final_i = 0
while dividend >= divisor:
temp_divisor = 0 + divisor
temp_i = 0.5
while dividend >= temp_divisor:
temp_i = temp_i + temp_i
dividend -= temp_divisor
temp_divisor = temp_divisor + temp_divisor
final_i += temp_i
return int(final_i)
if dividend < 0 and divisor < 0:
final_i = 0
while dividend <= divisor:
temp_divisor = 0 + divisor
temp_i = 0.5
while dividend <= temp_divisor:
temp_i = temp_i + temp_i
dividend -= temp_divisor
temp_divisor = temp_divisor + temp_divisor
final_i += temp_i
return int(final_i)
if dividend > 0 and divisor < 0:
final_i = 0
while (dividend + divisor) >= 0:
temp_divisor = 0 + divisor
temp_i = -0.5
while (dividend + temp_divisor) >= 0:
temp_i = temp_i + temp_i
dividend += temp_divisor
temp_divisor = temp_divisor + temp_divisor
final_i += temp_i
return int(final_i)
if dividend < 0 and divisor > 0:
final_i = 0
while (dividend + divisor) <= 0:
temp_divisor = 0 + divisor
temp_i = -0.5
while (dividend + temp_divisor) <= 0:
temp_i = temp_i + temp_i
dividend += temp_divisor
temp_divisor = temp_divisor + temp_divisor
final_i += temp_i
return int(final_i)