-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4Sum.py
24 lines (22 loc) · 801 Bytes
/
4Sum.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
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
twosum = collections.defaultdict(set)
onums = sorted(nums)
size = len(nums)
for i in range(size):
for j in range(i + 1, size):
twosum[onums[i] + onums[j]].add((i, j))
ret = set()
for i in range(size):
for j in range(i + 1, size):
left = target - onums[i] - onums[j]
if left in twosum:
for i_i, i_j in twosum[left]:
if j < i_i:
ret.add((onums[i], onums[j], onums[i_i], onums[i_j]))
return [list(k) for k in ret]