-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15. 3Sum.py
More file actions
38 lines (31 loc) · 1.01 KB
/
15. 3Sum.py
File metadata and controls
38 lines (31 loc) · 1.01 KB
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
from typing import List
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums = sorted(nums)
res = []
i = len(nums) - 1
while i > 1:
target = -nums[i]
j = 0
k = i-1
while j<k:
temp = nums[j] + nums[k]
if temp < target:
j += 1
elif temp > target:
k -= 1
else:
res.append([nums[j], nums[k], nums[i]])
j += 1
k -= 1
# remove duplicates
while j >= 1 and j < k and nums[j] == nums[j-1]:
j += 1
while k>j and k<i-1 and nums[k] == nums[k+1]:
k -= 1
i -= 1
while i > 1 and i < len(nums)-1 and nums[i] == nums[i+1]:
i -= 1
return res #list(set(res))
res = Solution().threeSum([-1,0,1,2,-1,-4])
print (res)