Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
627 B
2
Indexable
Never
def twoSum(arr, target):
    results = []
    seen = set()
    for x in arr:
        if target - x in seen:
            results.append((x, target - x, -target))
        
        seen.add(x)
    
    return results

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        results = []
        seen = set()
        for i in range(len(nums)):
            target = -nums[i]
            if target in seen:
                continue

            seen.add(target)

            res = twoSum(nums[i+1:], target)
            results.extend(res)

        return set([tuple(sorted(res)) for res in results])