-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.py
37 lines (37 loc) · 1.24 KB
/
18.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
34
35
36
37
class Solution:
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
k = len(nums)
nums.sort()
if k < 4 or nums[0] * 4 > target or nums[-1] * 4 < target:
return []
sol = []
for i in range(k - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
target1 = target - nums[i]
for j in range(i + 1, k - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
target2 = target1 - nums[j]
m = j + 1
n = k - 1
while m < n:
res = nums[m] + nums[n]
if res == target2:
sol.append([nums[i], nums[j], nums[m], nums[n]])
m += 1
n -= 1
while m < n and nums[m] == nums[m - 1]:
m += 1
while m < n and nums[n] == nums[n + 1]:
n -= 1
elif res > target2:
n -= 1
else:
m += 1
return sol