-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4sum.py
More file actions
28 lines (27 loc) · 913 Bytes
/
4sum.py
File metadata and controls
28 lines (27 loc) · 913 Bytes
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
def fourSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
d = dict()
for i in range(len(nums)):
for j in range(i+1,len(nums)):
sum2 = nums[i]+nums[j]
if sum2 in d:
d[sum2].append((i,j))
else:
d[sum2] = [(i,j)]
result = set()
for key in d:
value = target - key
if value in d:
list1 = d[key]
list2 = d[value]
for (i,j) in list1:
for (k,l) in list2:
if i!=k and i!=l and j!=k and j!=l:
flist = [nums[i],nums[j],nums[k],nums[l]]
flist.sort()
result.add(tuple(flist))
return list(result)