-
Notifications
You must be signed in to change notification settings - Fork 1
/
18_4sum.py
33 lines (30 loc) · 1.2 KB
/
18_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
25
26
27
28
29
30
31
32
33
class Solution():
def fourSum(self, nums, target):
"""
:param nums: List[int]
:param target: int
:return: List[List[int]]
"""
nums.sort()
result = []
result_set = set()
for left in xrange(len(nums) - 3):
for right in xrange(len(nums) - 1, 2, -1):
i = left + 1
j = right - 1
while i < j:
the_sum = nums[left] + nums[i] + nums[j] + nums[right]
if the_sum == target:
key = "%s_%s_%s_%s" % (nums[left], nums[i], nums[j], nums[right])
if key not in result_set:
result.append([nums[left], nums[i], nums[j], nums[right]])
result_set.add(key)
i += 1
j -= 1
elif the_sum > target:
j -= 1
else:
i += 1
return result
assert Solution().fourSum([1, 0, -1, 0, -2, 2], 0) == [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
assert Solution().fourSum([-5, 5, 4, -3, 0, 0, 4, -2], 4) == [[-5, 0, 4, 5], [-3, -2, 4, 5]]