-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathleetcode_018.py
More file actions
34 lines (30 loc) · 1.03 KB
/
leetcode_018.py
File metadata and controls
34 lines (30 loc) · 1.03 KB
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
# leetcode 18. 四数之和
class Solution:
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
lens = len(nums)
dic = {}
#先排序后用集合去重
res = set()
nums.sort()
#先计算前两个数的和
for i in range(lens-1):
for p in range(i+1,lens):
key = nums[i] + nums[p]
if key not in dic:
dic[key] = [(i, p)]
else:
dic[key].append((i, p))
for i in range(2,lens-1):
for p in range(i+1, lens):
pre = target - nums[i] - nums[p]
if pre in dic:
for index in dic[pre]:
#通过下标判断为合格的后两位数
if index[1] < i:
res.add((nums[index[0]], nums[index[1]],nums[i], nums[p]))
return [list(i) for i in res]