-
Notifications
You must be signed in to change notification settings - Fork 0
/
015.py
34 lines (32 loc) · 1.08 KB
/
015.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
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) < 3:
return []
nums.sort()
ans = []
for i in range(len(nums)-2):
if i > 0 and nums[i] == nums[i-1]:
continue
j, k = i+1, len(nums)-1
while j < k:
# print(ans, i, j, k)
tri_sum = nums[i] + nums[j] + nums[k]
if tri_sum == 0:
ans.append([nums[i], nums[j], nums[k]])
# to avoid duplicates at end
while j < k and k > 0 and nums[k] == nums[k-1]:
k -= 1
k -= 1
# to avoid duplicates at begin
while j < k and nums[j] == nums[j+1]:
j += 1
j += 1 # move both j k to another pos
elif tri_sum > 0:
k -= 1
else:
j += 1
return ans