-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path4sum.py
24 lines (24 loc) · 800 Bytes
/
4sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n = len(nums)
nums.sort()
res = set()
for i in range(n):
a = nums[i]
for j in range(i + 1, n - 2):
b = nums[j]
start = j + 1
end = n - 1
while start < end:
c = nums[start]
d = nums[end]
total = b + c + d
if total == target - a:
res.add(tuple([a, b, c, d]))
start += 1
end -= 1
elif total > target - a:
end -= 1
else:
start += 1
return list(res)