-
Notifications
You must be signed in to change notification settings - Fork 43
/
divide-two-integers.py
137 lines (127 loc) · 3.82 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# V0
# V1
# https://www.cnblogs.com/zuoyuan/p/3779359.html
# IDEA : BINARY SEARCH
class Solution:
# @return an integer
def divide(self, dividend, divisor):
if (dividend < 0 and divisor > 0) or (dividend > 0 and divisor < 0):
if abs(dividend) < abs(divisor):
return 0
sum = 0; count = 0; res = 0
a = abs(dividend); b = abs(divisor)
while a >= b:
sum = b
count = 1
while sum + sum <= a:
sum += sum
count += count
a -= sum
res += count
if (dividend < 0 and divisor > 0) or (dividend > 0 and divisor < 0):
res = 0 - res
return res
# V1'
# https://blog.csdn.net/NXHYD/article/details/72539880
class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
neg = (dividend >= 0) ^ (divisor >= 0)
dividend, divisor = abs(dividend), abs(divisor)
pos, base = 1, divisor
while base <= dividend:
pos <<= 1
base <<= 1
base >>= 1
pos >>= 1
res = 0
while pos > 0:
if base <= dividend:
res += pos
dividend -= base
base >>= 1
pos >>= 1
val = -res if neg else res
return 2 ** 31 -1 if val > 2 ** 31 -1 else val
# V1'''
# https://www.jiuzhang.com/solution/divide-two-integers/#tag-highlight-lang-python
class Solution(object):
def divide(self, dividend, divisor):
INT_MAX = 2147483647
if divisor == 0:
return INT_MAX
neg = dividend > 0 and divisor < 0 or dividend < 0 and divisor > 0
a, b = abs(dividend), abs(divisor)
ans, shift = 0, 31
while shift >= 0:
if a >= b << shift:
a -= b << shift
ans += 1 << shift
shift -= 1
if neg:
ans = - ans
if ans > INT_MAX:
return INT_MAX
return ans
# V1''''
class Solution(object):
def divide(self, dividend, divisor):
"""
Assume we are dealing with an environment
which could only store integers within the 32-bit
signed integer range: [-2**31, 2**31 − 1].
For the purpose of this problem, assume that your
function returns 2**31 − 1 when the division result overflows.
--> so the return value should 2**31 < always < 2**31 -1
(2**31 -1 = 2147483647, 2**31 = 2147483648)
"""
if dividend*divisor > 0:
return min(2147483647, dividend//divisor)
else:
return max(-2147483648, -(abs(dividend)//abs(divisor)))
# V2
# Time: O(logn) = O(1)
# Space: O(1)
class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
result, dvd, dvs = 0, abs(dividend), abs(divisor)
while dvd >= dvs:
inc = dvs
i = 0
while dvd >= inc:
dvd -= inc
result += 1 << i
inc <<= 1
i += 1
if dividend > 0 and divisor < 0 or dividend < 0 and divisor > 0:
return -result
else:
return result
def divide2(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
positive = (dividend < 0) is (divisor < 0)
dividend, divisor = abs(dividend), abs(divisor)
res = 0
while dividend >= divisor:
temp, i = divisor, 1
while dividend >= temp:
dividend -= temp
res += i
i <<= 1
temp <<= 1
if not positive:
res = -res
return min(max(-2147483648, res), 2147483647)