-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivide.py
More file actions
51 lines (48 loc) · 1.08 KB
/
divide.py
File metadata and controls
51 lines (48 loc) · 1.08 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
class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
if divisor == 0:
return 2147483647
ret = self.conmpute(abs(dividend),abs(divisor))
if dividend < 0 and divisor > 0:
if abs(dividend) % abs(divisor):
return 0-ret
else:
return 0-ret
elif divisor < 0 and dividend > 0:
# print ret
if abs(dividend) % abs(divisor):
return 0-ret
else:
return 0-ret
else:
if ret >= 2147483647:
return 2147483647
else:
return ret
def conmpute(self,dividend,divisor):
sol = 0
while dividend >= divisor:
shift = 0
currentdivisor = divisor
while dividend >= currentdivisor<<1:
currentdivisor <<=1
shift +=1
sol += 1<<shift
dividend -= divisor<<shift
if sol >= 2147483648:
return 2147483648
return sol
s = Solution()
div = -3
for x in range(3,100):
if s.divide(x,div) != x/div:
print x,s.divide(x,div),x/div
# print s.divide(-2147483648,-1)
# print s.divide(-2147483650, 1)
# print s.divide(1026117192, -874002063)
# print s.divide(-1039162657, 490823224)