1.题目描述
Given a set of candidate numbers (candidates
) (without duplicates) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
The same repeated number may be chosen from candidates
unlimited number of times.
给定一个无重复元素的数组candidates
和一个目标数target
,找出candidates
中所有可以使数字和为target
的组合。
candidates
中的数字可以无限制重复被选取。
Note:
All numbers (including
target
) will be positive integers.The solution set must not contain duplicate combinations.
说明:
- 所有数字(包括
target
)都是正整数。 - 解集不能包含重复的组合。
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
2.Solutions
1 | public static List<List<Integer>> combinationSum(int[] nums, int target) { |
3.举一反三
This structure might apply to many other backtracking questions, but here I am just going to demonstrate Subsets(子集), Permutations(全排列), Combination Sum(组合总和) and Palindrome Partitioning(分割回文串).
Subsets : https://leetcode.com/problems/subsets/
1 | public List<List<Integer>> subsets(int[] nums) { |
Subsets II (contains duplicates) : https://leetcode.com/problems/subsets-ii/
1 | public List<List<Integer>> subsetsWithDup(int[] nums) { |
Permutations : https://leetcode.com/problems/permutations/
1 | public List<List<Integer>> permute(int[] nums) { |
Permutations II (contains duplicates) : https://leetcode.com/problems/permutations-ii/
1 | public List<List<Integer>> permuteUnique(int[] nums) { |
Combination Sum : https://leetcode.com/problems/combination-sum/
1 | public List<List<Integer>> combinationSum(int[] nums, int target) { |
Combination Sum II (can’t reuse same element) : https://leetcode.com/problems/combination-sum-ii/
1 | public List<List<Integer>> combinationSum2(int[] nums, int target) { |
Palindrome Partitioning : https://leetcode.com/problems/palindrome-partitioning/
1 | public List<List<String>> partition(String s) { |