给你一个 无重复元素 的整数数组 candidates
和一个目标整数 target
,找出 candidates
中可以使数字和为目标数 target
的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates
中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target
的不同组合数少于 150
个。
示例 1:
输入:candidates =[2,3,6,7],
target =7
输出:[[2,2,3],[7]] 解释: 2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。 7 也是一个候选, 7 = 7 。 仅有这两种组合。
示例 2:
输入: candidates = [2,3,5],
target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2],
target = 1
输出: []
提示:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
candidates
的所有元素 互不相同1 <= target <= 40
我们可以先对数组进行排序,方便剪枝。
接下来,我们设计一个函数
在函数
我们也可以将函数
在主函数中,我们只要调用函数
时间复杂度
相似题目:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def dfs(i: int, s: int):
if s == 0:
ans.append(t[:])
return
if s < candidates[i]:
return
for j in range(i, len(candidates)):
t.append(candidates[j])
dfs(j, s - candidates[j])
t.pop()
candidates.sort()
t = []
ans = []
dfs(0, target)
return ans
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
private List<Integer> t = new ArrayList<>();
private int[] candidates;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
this.candidates = candidates;
dfs(0, target);
return ans;
}
private void dfs(int i, int s) {
if (s == 0) {
ans.add(new ArrayList(t));
return;
}
if (s < candidates[i]) {
return;
}
for (int j = i; j < candidates.length; ++j) {
t.add(candidates[j]);
dfs(j, s - candidates[j]);
t.remove(t.size() - 1);
}
}
}
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> t;
function<void(int, int)> dfs = [&](int i, int s) {
if (s == 0) {
ans.emplace_back(t);
return;
}
if (s < candidates[i]) {
return;
}
for (int j = i; j < candidates.size(); ++j) {
t.push_back(candidates[j]);
dfs(j, s - candidates[j]);
t.pop_back();
}
};
dfs(0, target);
return ans;
}
};
func combinationSum(candidates []int, target int) (ans [][]int) {
sort.Ints(candidates)
t := []int{}
var dfs func(i, s int)
dfs = func(i, s int) {
if s == 0 {
ans = append(ans, slices.Clone(t))
return
}
if s < candidates[i] {
return
}
for j := i; j < len(candidates); j++ {
t = append(t, candidates[j])
dfs(j, s-candidates[j])
t = t[:len(t)-1]
}
}
dfs(0, target)
return
}
function combinationSum(candidates: number[], target: number): number[][] {
candidates.sort((a, b) => a - b);
const ans: number[][] = [];
const t: number[] = [];
const dfs = (i: number, s: number) => {
if (s === 0) {
ans.push(t.slice());
return;
}
if (s < candidates[i]) {
return;
}
for (let j = i; j < candidates.length; ++j) {
t.push(candidates[j]);
dfs(j, s - candidates[j]);
t.pop();
}
};
dfs(0, target);
return ans;
}
impl Solution {
fn dfs(i: usize, s: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>) {
if s == 0 {
ans.push(t.clone());
return;
}
if s < candidates[i] {
return;
}
for j in i..candidates.len() {
t.push(candidates[j]);
Self::dfs(j, s - candidates[j], candidates, t, ans);
t.pop();
}
}
pub fn combination_sum(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
candidates.sort();
let mut ans = Vec::new();
Self::dfs(0, target, &candidates, &mut vec![], &mut ans);
ans
}
}
public class Solution {
private List<IList<int>> ans = new List<IList<int>>();
private List<int> t = new List<int>();
private int[] candidates;
public IList<IList<int>> CombinationSum(int[] candidates, int target) {
Array.Sort(candidates);
this.candidates = candidates;
dfs(0, target);
return ans;
}
private void dfs(int i, int s) {
if (s == 0) {
ans.Add(new List<int>(t));
return;
}
if (s < candidates[i]) {
return;
}
for (int j = i; j < candidates.Length; ++j) {
t.Add(candidates[j]);
dfs(j, s - candidates[j]);
t.RemoveAt(t.Count - 1);
}
}
}
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def dfs(i: int, s: int):
if s == 0:
ans.append(t[:])
return
if i >= len(candidates) or s < candidates[i]:
return
dfs(i + 1, s)
t.append(candidates[i])
dfs(i, s - candidates[i])
t.pop()
candidates.sort()
t = []
ans = []
dfs(0, target)
return ans
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
private List<Integer> t = new ArrayList<>();
private int[] candidates;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
this.candidates = candidates;
dfs(0, target);
return ans;
}
private void dfs(int i, int s) {
if (s == 0) {
ans.add(new ArrayList(t));
return;
}
if (i >= candidates.length || s < candidates[i]) {
return;
}
dfs(i + 1, s);
t.add(candidates[i]);
dfs(i, s - candidates[i]);
t.remove(t.size() - 1);
}
}
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> t;
function<void(int, int)> dfs = [&](int i, int s) {
if (s == 0) {
ans.emplace_back(t);
return;
}
if (i >= candidates.size() || s < candidates[i]) {
return;
}
dfs(i + 1, s);
t.push_back(candidates[i]);
dfs(i, s - candidates[i]);
t.pop_back();
};
dfs(0, target);
return ans;
}
};
func combinationSum(candidates []int, target int) (ans [][]int) {
sort.Ints(candidates)
t := []int{}
var dfs func(i, s int)
dfs = func(i, s int) {
if s == 0 {
ans = append(ans, slices.Clone(t))
return
}
if i >= len(candidates) || s < candidates[i] {
return
}
dfs(i+1, s)
t = append(t, candidates[i])
dfs(i, s-candidates[i])
t = t[:len(t)-1]
}
dfs(0, target)
return
}
function combinationSum(candidates: number[], target: number): number[][] {
candidates.sort((a, b) => a - b);
const ans: number[][] = [];
const t: number[] = [];
const dfs = (i: number, s: number) => {
if (s === 0) {
ans.push(t.slice());
return;
}
if (i >= candidates.length || s < candidates[i]) {
return;
}
dfs(i + 1, s);
t.push(candidates[i]);
dfs(i, s - candidates[i]);
t.pop();
};
dfs(0, target);
return ans;
}
impl Solution {
fn dfs(i: usize, s: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>) {
if s == 0 {
ans.push(t.clone());
return;
}
if i >= candidates.len() || s < candidates[i] {
return;
}
Self::dfs(i + 1, s, candidates, t, ans);
t.push(candidates[i]);
Self::dfs(i, s - candidates[i], candidates, t, ans);
t.pop();
}
pub fn combination_sum(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
candidates.sort();
let mut ans = Vec::new();
Self::dfs(0, target, &candidates, &mut vec![], &mut ans);
ans
}
}
public class Solution {
private List<IList<int>> ans = new List<IList<int>>();
private List<int> t = new List<int>();
private int[] candidates;
public IList<IList<int>> CombinationSum(int[] candidates, int target) {
Array.Sort(candidates);
this.candidates = candidates;
dfs(0, target);
return ans;
}
private void dfs(int i, int s) {
if (s == 0) {
ans.Add(new List<int>(t));
return;
}
if (i >= candidates.Length || s < candidates[i]) {
return;
}
dfs(i + 1, s);
t.Add(candidates[i]);
dfs(i, s - candidates[i]);
t.RemoveAt(t.Count - 1);
}
}