Skip to content

Commit 5ec1049

Browse files
committed
Leetcode 29-Medium-Divide-Two-Integers
Python Solution
1 parent 8f221f2 commit 5ec1049

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages!!
2525
| 26 | Easy | [Remove Duplicates from Sorted Array](leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/problem.md) | [C](leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/answer.c), [Python](leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/answer.py) |
2626
| 27 | Easy | [Remove Element](leetcode/27-Easy-Remove-Element/problem.md) | [Python](leetcode/27-Easy-Remove-Element/answer.py) |
2727
| 28 | Easy | [Implement strStr()](leetcode/28-Easy-Implement-strStr/problem.md) | [C](leetcode/28-Easy-Implement-strStr/answer.c), [Python](leetcode/28-Easy-Implement-strStr/answer.py) |
28+
| 29 | Medium | [Divide Two Integers](leetcode/29-Medium-Divide-Two-Integers/problem.md) | [Python](leetcode/29-Medium-Divide-Two-Integers/answer.py) |
2829
| 35 | Easy | [Search Insert Position](leetcode/35-Easy-Search-Insert-Position/problem.md) | [Go](leetcode/35-Easy-Search-Insert-Position/answer.go), [Java](leetcode/35-Easy-Search-Insert-Position/answer.java), [JavaScript](leetcode/35-Easy-Search-Insert-Position/answer.js), [Scala](leetcode/35-Easy-Search-Insert-Position/answer.scala) |
2930
| 36 | Medium | [Valid Sudoku](leetcode/36-Medium-Valid-Sudoku/problem.md) | [Python](leetcode/36-Medium-Valid-Sudoku/answer.py) |
3031
| 53 | Easy | [Maximum Subarray](leetcode/53-Easy-Maximum-Subarray/problem.md) | [Java](leetcode/53-Easy-Maximum-Subarray/answer.java) |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python
2+
3+
#------------------------------------------------------------------------------
4+
5+
class Solution:
6+
def divide(self, dividend, divisor):
7+
"""
8+
:type dividend: int
9+
:type divisor: int
10+
:rtype: int
11+
"""
12+
positive = (dividend < 0) is (divisor < 0)
13+
dividend, divisor = abs(dividend), abs(divisor)
14+
res = 0
15+
while dividend >= divisor:
16+
temp, i = divisor, 1
17+
while dividend >= temp:
18+
dividend -= temp
19+
res += i
20+
i <<= 1
21+
temp <<= 1
22+
if not positive:
23+
res = -res
24+
return min(max(-2147483648, res), 2147483647)
25+
26+
27+
#------------------------------------------------------------------------------
28+
#Testing
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Problem 29: Divide Two Integers
2+
3+
4+
#### Difficulty: Medium
5+
6+
#### Problem
7+
8+
Divide two integers without using multiplication, division and mod operator.
9+
10+
If it is overflow, return MAX_INT.
11+
12+
#### Example
13+
14+
<pre>
15+
16+
Division
17+
18+
</pre>

0 commit comments

Comments
 (0)