-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_5.py
More file actions
38 lines (35 loc) · 1.13 KB
/
15_5.py
File metadata and controls
38 lines (35 loc) · 1.13 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
38
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
def two_sum(nums: List[int], value: int) -> Set[Tuple[int]]:
size = len(nums)
i = 0
j = size - 1
result = set()
while i < j:
v = nums[i] + nums[j]
if v > value:
j -= 1
elif v < value:
i += 1
else:
t = tuple(sorted([nums[i], nums[j]]))
if t not in result:
result.add(t)
i += 1
j -= 1
return result
if not nums:
return []
size = len(nums)
if size < 3:
return []
nums.sort()
triplets = set()
for k in range(size):
n = nums[k+1:]
ts = two_sum(n, -nums[k])
for n1,n2 in ts:
triplet = tuple(sorted([n1, n2, nums[k]]))
if triplet not in triplets:
triplets.add(triplet)
return [[i,j,k] for i,j,k in triplets]