40. Combination Sum II
https://leetcode.com/problems/combination-sum-ii/
Related problems
39-combination-sum
46-permutations
47-permutations-ii
78-subsets
90-subsets-ii
---
Given a collection of candidate numbers (
candidates
) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
Each number in
candidates
may only be used once in the combination.
Note:
- All numbers (including
target
) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[10,1,2,7,6,1,5]
, target =8
, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [ [1,2,2], [5] ]---
Related problems
39-combination-sum
46-permutations
47-permutations-ii
78-subsets
90-subsets-ii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> combinationSum2(int[] candidates, int target) { | |
List<List<Integer>> ans = new ArrayList<List<Integer>>(); | |
Arrays.sort(candidates); | |
dfs(candidates, 0, target, new ArrayList<Integer>(), ans); | |
return ans; | |
} | |
private void dfs(int[] candidates, int curr, int target, List<Integer> prefix, List<List<Integer>> ans) { | |
if (target == 0) { | |
ans.add(new ArrayList<Integer>(prefix)); | |
return; | |
} | |
if (target < 0) { | |
return; | |
} | |
for (int i = curr; i < candidates.length; i++) { | |
// Skip equal element in the for loop, it will be used in recursed call | |
if (i > curr && candidates[i] == candidates[i - 1]) { | |
continue; | |
} | |
prefix.add(candidates[i]); | |
dfs(candidates, i + 1, target - candidates[i], prefix, ans); | |
prefix.remove(prefix.size() - 1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: | |
def dfs(prefix: List[int], i: int, target: int): | |
if target == 0: | |
ans.append(prefix.copy()) | |
return | |
for j in range(i, len(candidates)): | |
if j > i and candidates[j] == candidates[j - 1]: | |
continue | |
if target - candidates[j] >= 0: | |
prefix.append(candidates[j]) | |
dfs(prefix, j + 1, target - candidates[j]) | |
prefix.pop() | |
ans = [] | |
candidates = sorted(candidates) | |
dfs([], 0, target) | |
return ans |