-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivide Two Integers.py
More file actions
44 lines (41 loc) · 1.1 KB
/
Divide Two Integers.py
File metadata and controls
44 lines (41 loc) · 1.1 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
class Solution(object):
def divide(self, dividend, divisor):
maxR = 2147483647
minR = -2147483648
import sys
if divisor == 0:
return maxR
if dividend == 0:
return 0
isF = (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0)
dd = abs(dividend)
divisor = abs(divisor)
rtype = 0
if divisor == 1:
rtype = dd
elif divisor == 2:
rtype = dd >> 1
else:
divilist = map(lambda ch:int(ch),list(string(dividend)))
q = 0
r = 0
d = 0
for shu in divilist:
if d < divisor:
d = d*10 + shu
q = q*10
continue
tempq = 0
while d>=divisor:
tempq+=1
d -= divisor
tempq
if isF:
rtype = 0 - rtype
if rtype > maxR:
rtype = maxR
if rtype < minR:
rtype = minR
return rtype
s = Solution()
print s.divide(10,3)