-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.py
More file actions
37 lines (34 loc) · 980 Bytes
/
15.py
File metadata and controls
37 lines (34 loc) · 980 Bytes
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
from collections import defaultdict
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
n=len(nums)
ans=[]
for i in range(n):
if i>0 and nums[i]==nums[i-1]:
continue
j=i+1
k=n-1
while j<k:
total=nums[i]+nums[j]+nums[k]
if total>0:
k-=1
elif total<0:
j+=1
else:
ans.append([nums[i],nums[j],nums[k]])
while j<k:
j+=1
if nums[j]!=nums[j-1]:
break
while j<k:
k-=1
if nums[k]!=nums[k+1]:
break
return ans
nums =[-1,0,1,2,-1,-4]
print(Solution().threeSum(nums))