小米盒子做网站,一个县城广告公司利润,南京哪家公司做企业网站 做得比较好,男女做暧暧试看网站49回溯#xff1a;
1.先声明好大集合和小集合#xff0c;在调用回溯函数#xff0c;终止条件为sumtarget#xff0c;要进行剪枝操作减少遍历的次数#xff0c;去重操作防止数组中有两个相同的值来组成的集合相同。
class Solution {ListListInteger li1ne…回溯
1.先声明好大集合和小集合在调用回溯函数终止条件为sumtarget要进行剪枝操作减少遍历的次数去重操作防止数组中有两个相同的值来组成的集合相同。
class Solution {ListListInteger li1new ArrayListListInteger();ListInteger li2new ArrayListInteger();public ListListInteger combinationSum2(int[] candidates, int target) {//接收计算总和int sum0;//方便去重操作Arrays.sort(candidates);huisu(candidates,target,0,sum);return li1;}public void huisu(int[] candidates,int target,int Index,int sum){//终止条件if(sumtarget ){li1.add(new ArrayList(li2));return ;}//遍历和剪枝操作for(int iIndex;icandidates.lengthsumcandidates[i]target;i){//去重操作if (iIndexcandidates[i]candidates[i-1]){continue;} //加入集合li2中li2.add(candidates[i]);sumcandidates[i];//嵌套huisu(candidates,target,i1,sum);//回溯操作sum-li2.get(li2.size()-1);li2.removeLast();}}
}