-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path18. 4Sum.py
26 lines (26 loc) · 1.19 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
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
ans=[]
nums.sort()
for i in range(len(nums)-3):
if i>0 and nums[i]==nums[i-1]:
continue
for j in range(i+1,len(nums)-2):
if j>i+1 and nums[j]==nums[j-1]:
continue
tsum = target-(nums[i]+nums[j])
lo,hi=j+1,len(nums)-1
while lo<hi:
if tsum == nums[lo]+nums[hi]:
ans.append([nums[i],nums[j],nums[lo],nums[hi]])
while lo<hi and nums[lo]==nums[lo+1]:
lo+=1
while lo<hi and nums[hi]==nums[hi-1]:
hi-=1
lo+=1
hi-=1
elif tsum > nums[lo]+nums[hi]:
lo+=1
else:
hi-=1
return ans