-
Notifications
You must be signed in to change notification settings - Fork 80
/
three_sum.py
54 lines (46 loc) · 1.52 KB
/
three_sum.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# coding: utf-8
"""
https://leetcode.com/problems/3sum/
"""
from typing import List
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
dedup_set = set(nums)
length_dedup_set = len(dedup_set)
if length_dedup_set <= 1:
if sum(dedup_set) == 0 and len(nums) >= 3:
return [[0, 0, 0]]
return []
nums.sort()
# there might be multiple results
def twoSum(nums, target):
results = []
index_dict = {}
for num in nums:
complement = target - num
if complement in index_dict:
results.append([num, complement])
else:
index_dict[num] = None
return results
results = []
results_set = set()
first_nums = set()
for i, num in enumerate(nums):
if num in first_nums:
continue
else:
first_nums.add(num)
if num == 0:
target = 0
else:
target = -num
two_nums_lists = twoSum(nums[i + 1:], target)
if two_nums_lists:
for two_nums_list in two_nums_lists:
result = sorted([num, ] + two_nums_list)
result_tuple = tuple(result)
if result_tuple not in results_set:
results_set.add(result_tuple)
results.append(result)
return results