Skip to content

Commit e28740d

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 18
1 parent 66b3c57 commit e28740d

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
- [15 3Sum](https://leetcode.com/problems/3sum/description/)
8181
- [16 3Sum Closest](https://leetcode.com/problems/3sum-closest/description/)
8282
- [17 Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)
83+
- [18 4Sum](https://leetcode.com/problems/4sum/description/)
8384
- [19 Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
8485
- [20 Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/)
8586
- [21 Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
8+
"""
9+
Given an array nums of n integers, return an array of all the unique quadruplets
10+
[nums[a], nums[b], nums[c], nums[d]] such that:
11+
12+
- 0 <= a, b, c, d < n
13+
- a, b, c, and d are distinct.
14+
- nums[a] + nums[b] + nums[c] + nums[d] == target
15+
16+
You may return the answer in any order.
17+
"""
18+
nums = sorted(nums)
19+
fourSums = []
20+
i = 0
21+
while i < len(nums) - 3:
22+
if i > 0 and nums[i] == nums[i - 1]:
23+
i += 1
24+
continue
25+
j = i + 1
26+
while j < len(nums) - 2:
27+
if j > i + 1 and nums[j] == nums[j - 1]:
28+
j += 1
29+
continue
30+
left, right = j + 1, len(nums) - 1
31+
while left < right:
32+
fourSum = nums[i] + nums[j] + nums[left] + nums[right]
33+
if fourSum > target:
34+
right -= 1
35+
elif fourSum < target:
36+
left += 1
37+
else:
38+
fourSums.append([nums[i], nums[j], nums[left], nums[right]])
39+
left += 1
40+
while left < right and nums[left] == nums[left - 1]:
41+
left += 1
42+
j += 1
43+
i += 1
44+
return fourSums

tests/test_18_four_sum.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._18_four_sum import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "target", "expected"],
10+
argvalues=[
11+
([1, 0, -1, 0, -2, 2], 0, [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]),
12+
([2, 2, 2, 2, 2], 8, [[2, 2, 2, 2]]),
13+
([-2, -1, -1, 1, 1, 2, 2], 0, [[-2, -1, 1, 2], [-1, -1, 1, 1]]),
14+
],
15+
)
16+
def test_func(nums: List[int], target: int, expected: List[List[int]]):
17+
"""Tests the solution of a LeetCode problem."""
18+
four_sums = Solution().fourSum(nums, target)
19+
assert four_sums == expected

0 commit comments

Comments
 (0)