-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path18.py
More file actions
28 lines (28 loc) · 904 Bytes
/
Copy path18.py
File metadata and controls
28 lines (28 loc) · 904 Bytes
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
class Solution:
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
size = len(nums)
if size < 4:
return []
nums.sort()
ret = []
nums_set = set()
for i in range(size - 3):
for j in range(i + 1, size - 2):
k, l = j + 1, size - 1
while k < l:
sum = nums[i] + nums[j] + nums[k] + nums[l]
if sum > target:
l -= 1
elif sum < target:
k += 1
else:
if [nums[i], nums[j], nums[k], nums[l]] not in ret:
ret.append([nums[i], nums[j], nums[k], nums[l]])
k += 1
l -= 1
return ret