-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15.py
30 lines (25 loc) · 967 Bytes
/
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
class Solution(object):
# threeSum transfer twoSum : a + b = -c
# 有点类似于快排的思想
def threeSum(self, nums):
nums.sort() # 先将列表元素进行排序
N, results = len(nums), []
for i in range(N):
if i > 0 and nums[i] == nums[i-1]: # 去除相同的结果
continue
target = nums[i] * -1
s, e = i+1, N-1
while s < e:
if nums[s] + nums[e] == target:
results.append([nums[i], nums[s], nums[e]])
s = s + 1
while s < e and nums[s] == nums[s-1]: # 去除相同的结果
s = s + 1
elif nums[s] + nums[e] < target:
s = s + 1
else:
e = e - 1
return results
if __name__ == "__main__":
nums = [-1, 0, 1, 2, -1, -4]
print(Solution().threeSum(nums))