1+ class Solution (object ):
2+ def threeSum (self , nums ):
3+ """
4+ :type nums: List[int]
5+ :rtype: List[List[int]]
6+ """
7+ collectSum = []
8+
9+ if len (nums ) < 3 or len (nums ) > 3000 :
10+ return collectSum
11+
12+ totalNum = len (nums )
13+ nums = sorted (nums )
14+
15+ for i in range (totalNum - 2 ):
16+ if i > 0 and nums [i ] == nums [i - 1 ]:
17+ continue
18+
19+ iLeft , iRight = i + 1 , totalNum - 1
20+ while iLeft < iRight :
21+ listSumNum = sorted ([nums [iLeft ], nums [i ], nums [iRight ]])
22+ sumNum = sum (listSumNum )
23+ if sumNum == 0 :
24+ collectSum .append (listSumNum )
25+ while iLeft < iRight and nums [iLeft ] == nums [iLeft + 1 ]:
26+ iLeft += 1
27+
28+ while iLeft < iRight and nums [iRight ] == nums [iRight - 1 ]:
29+ iRight -= 1
30+
31+ iLeft += 1
32+ iRight -= 1
33+ elif sumNum < 0 :
34+ iLeft += 1
35+ else :
36+ iRight -= 1
37+
38+ def sortListNum (num ):
39+ return num [2 ]
40+
41+ collectSum .sort (key = sortListNum )
42+
43+ return collectSum
44+
45+
46+ print (Solution ().threeSum ([- 1 , 0 , 1 , 2 , - 1 , - 4 ])) #[[-1,-1,2],[-1,0,1]]
47+ print (Solution ().threeSum ([0 , 0 , 0 ])) #[[0,0,0]]
48+ print (Solution ().threeSum ([3 , - 2 , 1 , 0 ])) #[]
49+ print (Solution ().threeSum ([1 , - 1 , - 1 , 0 ])) #[[-1,0,1]]
50+ print (Solution ().threeSum ([- 1 , 0 , 1 , 0 ])) #[[-1,0,1]]
51+ print (Solution ().threeSum ([2 , - 3 , 0 , - 2 , - 5 , - 5 , - 4 , 1 , 2 , - 2 , 2 , 0 , 2 , - 4 , 5 , 5 , - 10 ]))
52+ #[[-10,5,5],[-5,0,5],[-4,2,2],[-3,-2,5],[-3,1,2],[-2,0,2]]
53+
0 commit comments