-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3SUM.py
27 lines (25 loc) · 979 Bytes
/
3SUM.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 threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
arr=[]
for i in range (len(nums)-2):
if i > 0 and nums[i]==nums[i-1]:
continue
start =i+1
end =len(nums)-1
while start<end:
threesum=nums[i]+nums[start]+nums[end]
if threesum >0:
end =end-1
elif threesum<0:
start=start+1
elif threesum==0:
# if [nums[i],nums[end],nums[start]] not in arr:
arr.append([nums[i],nums[start],nums[end]])
while end>start and nums[start+1]==nums[start]:
start =start+1
while end>start and nums[end]==nums[end-1] :
end =end -1
start += 1
end -= 1
return arr