forked from shubham7668/hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3 sum in Python
32 lines (32 loc) · 1004 Bytes
/
3 sum in Python
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
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
i=0
ans=[]
n=len(nums)
while i<n:
left=i+1
right=n-1
target2=-nums[i]
while left<right:
lrsum=nums[left]+nums[right]
if lrsum<target2:
left+=1
elif lrsum>target2:
right-=1
else:
ia=[]
ia.append(nums[i])
ia.append(nums[left])
ia.append(nums[right])
ans.append(ia)
left+=1
while left<right and nums[left]==nums[left-1]:
left+=1
right-=1
while left<right and nums[right]==nums[right+1]:
right-=1
i+=1
while i<n and nums[i]==nums[i-1]:
i+=1
return ans