Skip to content

Commit 8cd59d8

Browse files
committed
add python solution for Problem 29
1 parent 509deda commit 8cd59d8

File tree

2 files changed

+31
-0
lines changed

2 files changed

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

solution/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
├── 0029.Divide Two Integers
165165
│   ├── README.md
166166
│   └── Solution.java
167+
│   └── Solution.py
167168
├── 0030.Substring with Concatenation of All Words
168169
│   ├── README.md
169170
│   └── Solution.java

0 commit comments

Comments
 (0)