Skip to content

Commit 75349e0

Browse files
authored
Merge pull request doocs#195 from ashmichheda/master
add python solution for Problem 29
2 parents 366f28d + 4e18c0c commit 75349e0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def divide(self, dividend, divisor):
3+
"""
4+
:type dividend: int
5+
:type divisor: int
6+
:rtype: int
7+
"""
8+
if dividend == 0:
9+
return 0
10+
if divisor == 0:
11+
return 2 ** 31 - 1
12+
13+
sign = (dividend < 0) ^ (divisor < 0)
14+
15+
quotient = 0
16+
dividend = abs(dividend)
17+
divisor = abs(divisor)
18+
while dividend >= divisor:
19+
tmp, i = divisor, 1
20+
while dividend >= tmp:
21+
dividend -= tmp
22+
quotient += i
23+
tmp <<= 1
24+
print('Value of temp: '+str(tmp))
25+
i <<= 1
26+
print('Value of i: '+str(i))
27+
quotient *= (-1) ** sign
28+
29+
return min(max(quotient, - 2 ** 31), 2 ** 31 - 1)

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
├── 0029.Divide Two Integers
170170
│   ├── README.md
171171
│   └── Solution.java
172+
│   └── Solution.py
172173
├── 0030.Substring with Concatenation of All Words
173174
│   ├── README.md
174175
│   └── Solution.java

0 commit comments

Comments
 (0)