-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
43 lines (40 loc) · 1.58 KB
/
answer.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
#!/usr/bin/python3
#------------------------------------------------------------------------------
class Solution:
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
# Each key in pairs will be the sum of the pair
# Values will be tuples that sum to the key
pairs = {}
# Find sums for each pair
for i in range(len(nums)):
for j in range(i+1, len(nums)):
pair_sum = nums[i] + nums[j]
if pair_sum in pairs:
pairs[pair_sum].append((i,j))
else:
pairs[pair_sum] = [(i,j)]
result = []
# Find two pairs that equal the target and append to list
for pair in pairs:
value = target - pair
if value in pairs:
first = pairs[pair]
second = pairs[value]
for (i,j) in first:
for (k,l) in second:
# Ensure none of the same elements are used
# ijkl are indices
if i!=k and i!=l and j!=k and j!=l:
flist = [nums[i],nums[j],nums[k],nums[l]]
# Sort to prevent dups
flist.sort()
if tuple(flist) not in result:
result.append(tuple(flist))
return result
#------------------------------------------------------------------------------
#Testing