Skip to content

Commit 048b1c8

Browse files
author
wuduhren
committed
combination-sum-ii
1 parent 06dd433 commit 048b1c8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
3+
def helper(i, target):
4+
if target==0:
5+
ans.append(combination.copy())
6+
return
7+
8+
if i==len(candidates) or candidates[i]>target:
9+
return
10+
11+
combination.append(candidates[i])
12+
helper(i+1, target-candidates[i])
13+
combination.pop()
14+
15+
while i+1<len(candidates) and candidates[i]==candidates[i+1]: i += 1
16+
helper(i+1, target)
17+
18+
ans = []
19+
combination = []
20+
candidates.sort()
21+
helper(0, target)
22+
return ans

problems/python3/subsets-ii.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Time: O(N * 2^N)
3+
Space: O(N)
4+
"""
15
class Solution:
26
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
37
def helper(i):

0 commit comments

Comments
 (0)