-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum.py
40 lines (31 loc) · 1.38 KB
/
3sum.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
35
36
37
38
39
40
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 2:
return []
# trivial triplet
if len(nums) == 3:
if sum(nums) == 0:
return [nums]
else:
return []
# store values and indices
hash_map = {v:i for i, v in enumerate(nums)}
triplets = []
hashes = set() # for getting unique triplets
for i in range(len(nums)):
for j in range(i+1, len(nums)):
partial_sum = nums[i] + nums[j] # twosum
# get index of the third element based on twosum
try:
k = hash_map[-partial_sum]
except KeyError:
continue
# check constraints, i!=j not required since j moves ahead of i
if k != i and k != j:
# compute permutation invariant hash of the triplet
total_hash_sum = hash(frozenset([nums[i], nums[j], nums[k]]))
# add unique triplet
if total_hash_sum not in hashes:
triplets.append([nums[i], nums[j], nums[k]])
hashes.add(total_hash_sum)
return triplets