We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cefa7c9 commit af1d46cCopy full SHA for af1d46c
3sum/printjin-gmailcom.py
@@ -1,12 +1,14 @@
1
from typing import List
2
+from itertools import combinations
3
class Solution:
4
def threeSum(self, nums):
5
+ res = set()
6
n = len(nums)
- result = set()
7
for i in range(n):
- for j in range(i + 1, n):
8
- for k in range(j + 1, n):
9
- if nums[i] + nums[j] + nums[k] == 0:
10
- triplet = tuple(sorted([nums[i], nums[j], nums[k]]))
11
- result.add(triplet)
12
- return [list(t) for t in result]
+ a = nums[i]
+ rest = nums[:i] + nums[i+1:]
+ for comb in combinations(rest, 2):
+ if sum(comb) == -a:
+ triplet = tuple(sorted([a, *comb]))
13
+ res.add(triplet)
14
+ return [list(t) for t in res]
0 commit comments