Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 0018/4Sums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
ans = set()
nums.sort()
for i in range(len(nums) - 3):
for j in range(i + 1, len(nums) - 2):
lo = j + 1
hi = len(nums) - 1
while lo < hi:
s = nums[i] + nums[j] + nums[lo] + nums[hi]
if s == target:
ans.add((nums[i], nums[j], nums[lo], nums[hi]))
lo += 1
elif s < target:
lo += 1
else:
hi -= 1
return list(ans)
33 changes: 33 additions & 0 deletions 0018/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 4Sum

Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.



## Example 1:

Input: nums = [1,0,-1,0,-2,2],

target = 0

Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

## Example 2:

Input: nums = [2,2,2,2,2],

target = 8

Output: [[2,2,2,2]]


## Constraints:

1 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109