forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_三数之和.py
38 lines (36 loc) · 1.05 KB
/
15_三数之和.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
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 09:30:27 2020
@author: leiya
"""
#sort+三指针
#一个指针从头开始,另外两个指针一左一右夹逼寻找,这也是sort的意义
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
res=[]
if(not nums or n<3):
return []
nums.sort()
res=[]
for i in range(n-2):
if(nums[i]>0):
return res
if(i>0 and nums[i]==nums[i-1]):
continue
L=i+1
R=n-1
while(L<R):
if(nums[i]+nums[L]+nums[R]==0):
res.append([nums[i],nums[L],nums[R]])
while(L<R and nums[L]==nums[L+1]):
L=L+1
while(L<R and nums[R]==nums[R-1]):
R=R-1
L=L+1
R=R-1
elif(nums[i]+nums[L]+nums[R]>0):
R=R-1
else:
L=L+1
return res