郑州网站推,wordpress 更换首页,亚马逊官网中国网页版,商河县建设局网站给定一个无重复元素的数组 candidates 和一个目标数 target #xff0c;找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明#xff1a;
所有数字#xff08;包括 target#xff09;都是正整数。 解集不能包含重复的…给定一个无重复元素的数组 candidates 和一个目标数 target 找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明
所有数字包括 target都是正整数。 解集不能包含重复的组合。 示例 1
输入candidates [2,3,6,7], target 7, 所求解集为 [ [7], [2,2,3] ]
代码
class Solution {ListListInteger cListnew ArrayList();public ListListInteger combinationSum(int[] candidates, int target) {combinationS(candidates,target,new ArrayList());return cList;}public void combinationS(int[] candidates, int target,ListInteger temp) {if(target0)//找到满足条件的序列{cList.add(new ArrayList(temp));return;}for(int i0;icandidates.length;i){if(targetcandidates[i]||temp.size()0candidates[i]temp.get(temp.size()-1))continue;//通过筛选升序的序列去重temp.add(candidates[i]);combinationS(candidates,target-candidates[i],temp);temp.remove(temp.size()-1);//回溯}}
}