-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_4Sum.py
More file actions
65 lines (40 loc) · 1.44 KB
/
18_4Sum.py
File metadata and controls
65 lines (40 loc) · 1.44 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 18. 4Sum
def threeSum(nums, k, three_Sum):
triplets = []
for i in range(k, len(nums)-2):
low = i+1
high = len(nums)-1
twoSum = three_Sum-nums[i]
while(low<high):
if nums[low]+nums[high]==twoSum:
if [nums[i], nums[low], nums[high]] not in triplets:
triplets.append([nums[i], nums[low], nums[high]])
while(low<high and nums[low]==nums[low+1]):
low+=1
while(low<high and nums[high]==nums[high-1]):
high-=1
low = low+1
high = high-1
elif nums[low]+nums[high]>twoSum:
high-=1
else:
low+=1
return triplets
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
quads = []
if len(nums)<4:
return []
if all(x==0 for x in nums):
if target == 0:
return [[0]*len(nums)]
else:
return []
nums = sorted(nums)
for i in range(len(nums)-3):
three_Sum = target-nums[i]
trip = threeSum(nums, i+1, three_Sum)
for j in range(len(trip)):
if [nums[i]]+trip[j] not in quads:
quads.append([nums[i]]+trip[j])
return quads