Skip to content

Instantly share code, notes, and snippets.

@cashlo
Last active January 14, 2020 19:29
Show Gist options
  • Save cashlo/74002d194969e46fbe310d9bcfe86e87 to your computer and use it in GitHub Desktop.
Save cashlo/74002d194969e46fbe310d9bcfe86e87 to your computer and use it in GitHub Desktop.
from collections import defaultdict
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums_count_map = defaultdict(int)
result_set = set()
for n in nums:
nums_count_map[n] += 1
for n in nums_count_map:
for m in nums_count_map:
if n == m and nums_count_map[n] < 2:
continue
target = 0 - m - n
if target in nums_count_map:
if target == n == m and nums_count_map[target] < 3:
continue
if (target == n or target == m) and nums_count_map[target] < 2:
continue
result_set.add( tuple(sorted([target, n, m])) )
return result_set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment