网站做二级域名干什么用,大连住房和建设局网站,广告设计公司深圳营销策划公司,潍坊企业网站模板建站题目链接#xff1a;LeetCode-39-组合总和 解题思路#xff1a; 先排序#xff0c;会节省时间#xff1b;由于数组中的数字可以无限制重复被选#xff0c;#xff0c;因此和前几道题的差别是index不需要1#xff0c;而是可以继续选择当前的元素 代码实现#xff1a; cl…题目链接LeetCode-39-组合总和 解题思路 先排序会节省时间由于数组中的数字可以无限制重复被选因此和前几道题的差别是index不需要1而是可以继续选择当前的元素 代码实现 class Solution {ListListInteger res new ArrayList();// 创建两个全局变量ListInteger path new ArrayList();public ListListInteger combinationSum(int[] candidates, int target) {Arrays.sort(candidates);backTracking(candidates, target, 0,0);return res;}public void backTracking(int[] candidates, int target, int index, int curSum){if (target curSum){res.add(new ArrayList(path));return;}for (int i index; i candidates.length; i) {if (curSumcandidates[i] target){// 这种情况也要考虑到continue;}path.add(candidates[i]);backTracking(candidates,target,i,curSumcandidates[i]);path.remove(path.size()-1);}}
}