forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4sum.py
More file actions
executable file
·33 lines (31 loc) · 1.08 KB
/
4sum.py
File metadata and controls
executable file
·33 lines (31 loc) · 1.08 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
"""
Time: O(N^(K-1)) -> O(N^3)
Space: O(K) -> O(1)
"""
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
def kSum(k, start, target):
if k>2:
for i in range(start, len(nums)-k+1):
if i!=start and nums[i]==nums[i-1]: continue
temp.append(nums[i])
kSum(k-1, i+1, target-nums[i])
temp.pop()
else:
l, r = start, len(nums)-1
while l<r:
if nums[l]+nums[r]>target:
r -= 1
elif nums[l]+nums[r]<target:
l += 1
else:
ans.append(temp+[nums[l], nums[r]])
while l<r and nums[l+1]==nums[l]: l += 1
while l<r and nums[r-1]==nums[r]: r -= 1
l += 1
r -= 1
ans = []
temp = []
nums.sort()
kSum(4, 0, target)
return ans