-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0018_Hash&Sort.py
32 lines (29 loc) · 1.02 KB
/
Leetcode0018_Hash&Sort.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
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
if not nums:
return []
if len(nums) <=3:
return []
nums.sort()
output = []
for i in range(len(nums)):
if i!=0 and nums[i]==nums[i-1]:
continue
for j in range(i+1, len(nums)):
if j!=(i+1) and nums[j]==nums[j-1]:
continue
hashmap = set()
p = j+1
while(p<len(nums)):
if (target-nums[i]-nums[j]-nums[p]) in hashmap:
output.append([nums[i],nums[j],nums[p],target-nums[i]-nums[j]-nums[p]])
while(p+1<len(nums) and nums[p+1]==nums[p]):
p += 1
hashmap.add(nums[p])
p += 1
return output