Skip to content

Commit aabce18

Browse files
chlng27
1 parent 48e8b3a commit aabce18

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Challenges/2021/February-LeetCoding-Challenge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ None
5151
|856.|[Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/)|~~Python~~|Medium|
5252
|581.|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Python](/Medium/581.ShortestUnsortedContinuousSubarray.py)|Medium|
5353
|946.|[Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/)|[Python](/Medium/946.ValidateStackSequences.py)|Medium|
54+
|29.|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[Python](/Medium/29.DivideTwoIntegers.py)|Medium|
5455

5556
## License
5657
The code is open-source and licensed under the [MIT License](/LICENSE).

Medium/29.DivideTwoIntegers.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Given two integers dividend and divisor, divide two
3+
integers without using multiplication, division, and
4+
mod operator.
5+
6+
Return the quotient after dividing dividend by divisor.
7+
8+
The integer division should truncate toward zero, which
9+
means losing its fractional part. For example,
10+
truncate(8.345) = 8 and truncate(-2.7335) = -2.
11+
12+
Note:
13+
- Assume we are dealing with an environment that
14+
could only store integers within the 32-bit signed
15+
integer range: [−2^31, 2^31 − 1]. For this problem,
16+
assume that your function returns 2^31 − 1 when the
17+
division result overflows.
18+
19+
Example:
20+
Input: dividend = 10, divisor = 3
21+
Output: 3
22+
Explanation: 10/3 = truncate(3.33333..) = 3.
23+
24+
Example:
25+
Input: dividend = 7, divisor = -3
26+
Output: -2
27+
Explanation: 7/-3 = truncate(-2.33333..) = -2.
28+
29+
Example:
30+
Input: dividend = 0, divisor = 1
31+
Output: 0
32+
33+
Example:
34+
Input: dividend = 1, divisor = 1
35+
Output: 1
36+
37+
Constraints:
38+
- -2^31 <= dividend, divisor <= 2^31 - 1
39+
- divisor != 0
40+
'''
41+
#Difficulty: Medium
42+
#989 / 989 test cases passed.
43+
#Runtime: 28 ms
44+
#Memory Usage: 14.3 MB
45+
46+
#Runtime: 28 ms, faster than 92.03% of Python3 online submissions for Divide Two Integers.
47+
#Memory Usage: 14.3 MB, less than 58.58% of Python3 online submissions for Divide Two Integers.
48+
49+
class Solution:
50+
def divide(self, dividend: int, divisor: int) -> int:
51+
min_number = -2**31
52+
max_number = 2**31 - 1
53+
result = abs(dividend) // abs(divisor)
54+
if (dividend > 0 and divisor > 0) or (dividend < 0 and divisor < 0):
55+
return min(result, max_number)
56+
else:
57+
return max(-result, min_number)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In this repository provided my Python solutions of LeetCode problems.
2020

2121
2021:
2222
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
23-
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 21/28
23+
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 22/28
2424
<br><br>
2525
## Solutions
2626
*P.S. If you like this, please leave me a star.*
@@ -49,6 +49,7 @@ In this repository provided my Python solutions of LeetCode problems.
4949
|25.|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)|[Python](/Hard/25.ReverseNodesink-Group.py)|Hard|`Linked List`, `Stack`|
5050
|26.|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)|[Python](https://github.com/YuriSpiridonov/LeetCode/blob/master/Easy/26.RemoveDuplicatesfromSortedArray.py)|Easy|Several solutions in one file|
5151
|27.|[Remove Element](https://leetcode.com/problems/remove-element/)|[Python](https://github.com/YuriSpiridonov/LeetCode/blob/master/Easy/27.RemoveElement.py)|Easy|
52+
|29.|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[Python](/Medium/29.DivideTwoIntegers.py)|Medium|
5253
|33.|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[Python](https://github.com/YuriSpiridonov/30-Day-LeetCoding-Challenge/blob/master/Week%203/SearchinRotatedSortedArray.py)|Medium|
5354
|34.|[Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)|[Python](/Medium/34.FindFirstandLastPositionofElementinSortedArray(BinarySearch).py)|Medium|`Binary Search`|
5455
|34.|[Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)|[Python](https://github.com/YuriSpiridonov/LeetCode/blob/master/Medium/34.FindFirstandLastPositionofElementinSortedArray.py)|Medium|

0 commit comments

Comments
 (0)