-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.3SumV2.py
More file actions
36 lines (35 loc) · 992 Bytes
/
Copy path15.3SumV2.py
File metadata and controls
36 lines (35 loc) · 992 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
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res=[]
nums.sort()
m=len(nums)-2
for i in range(m):
if i>0 and nums[i]==nums[i-1]:
continue
l,h=i+1,m+1
while l<h:
s=nums[i]+nums[l]+nums[h]
if s==0:
res.append([nums[i],nums[l],nums[h]])
while l<h and nums[h]==nums[h-1]:
h=h-1
while l<h and nums[l]==nums[l+1]:
l=l+1
h=h-1
l=l+1
elif s<0:
l=l+1
else :
h=h-1
return res
def main():
nums=[-1,0,1,2,-1,-4]
solution=Solution()
result=solution.threeSum(nums)
print(result)
if __name__=='__main__':
main()