其他分享
首页 > 其他分享> > leetcode39.组合求和

leetcode39.组合求和

作者:互联网

``java
public List<List> combinationSum(int[] candidates, int target) {
List<List> ans = new ArrayList<>();
List combine = new ArrayList<>();
dfs(candidates, target, combine, ans, 0);
return ans;
}

public void dfs(int[] candidates, int target, List combine, List<List> ans, int idx) {
if(idx == candidates.length) return;
if(0 == target) {
ans.add(new ArrayList<>(combine));
return;
}
dfs(candidates, target, combine, ans, idx+1);
if(target - candidates[idx] >= 0){
combine.add(candidates[idx]);
dfs(candidates, target-candidates[idx], combine, ans, idx);
combine.remove(combine.size()-1);
}
}
``

标签:target,组合,求和,leetcode39,idx,int,candidates,ans,combine
来源: https://www.cnblogs.com/huangming-zzz/p/15859549.html