forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
27 lines (27 loc) · 1.04 KB
/
Solution.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
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n, res = len(nums), []
if n < 4:
return []
nums.sort()
for i in range(n - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i + 1, n - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
k, l = j + 1, n - 1
while k < l:
if nums[i] + nums[j] + nums[k] + nums[l] == target:
res.append([nums[i], nums[j], nums[k], nums[l]])
k += 1
l -= 1
while k < n and nums[k] == nums[k - 1]:
k += 1
while l > j and nums[l] == nums[l + 1]:
l -= 1
elif nums[i] + nums[j] + nums[k] + nums[l] < target:
k += 1
else:
l -= 1
return res