首页 > 其他分享 >LeetCode 40.组合总和II

LeetCode 40.组合总和II

时间:2023-04-24 23:01:39浏览次数:48  
标签:used target int sum 40 II candidates path LeetCode

1.题目:

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。


示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8, 输出: [ [1,1,6], [1,2,5], [1,7], [2,6] ]

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/combination-sum-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


2.代码:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] used;//定义一个boolean类型的数组,来判断这个数组的元素是否遍历了
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];
         //Arrays.fill(used, false);//不赋值的话,初始值也是false
        if(candidates.length==0) return result;
        Arrays.sort(candidates);//先把数组进行排序,方便去重
        backtracking(candidates,target,0,0);
        return result;

    }
    public void backtracking(int[] candidates,int target,int sum,int startIndex){
     //   if(sum>startIndex) return;
        if(sum==target) {//满足条件就放到结果集当中
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i=startIndex; i<candidates.length; i++){
            if((sum+candidates[i])>target) break;//剪枝,当和大于target之后就直接跳出循环
            //去重的操作,注意要有i>0的初始条件,然后就是后一个元素跟前一个元素相同时,并且保证这一层循环是树层,而不是树枝,所以就是false
            if(i>0 && candidates[i]==candidates[i-1] && used[i-1]==false) continue;//重复的就跳过这次循环
            path.add(candidates[i]);//添加到一个暂时的集合里面去
            sum+=candidates[i];//累加
            used[i]=true;//更新used的值,声明这个元素已经用过了
            backtracking(candidates,target,sum,i+1);//递归
            path.remove(path.size()-1);//集合回溯
            sum-=candidates[i];//sum回溯(回退)
            used[i]=false;//数组回溯
            
        }
    }
}






标签:used,target,int,sum,40,II,candidates,path,LeetCode
From: https://blog.51cto.com/u_15806469/6222008

相关文章

  • leetcode 550 游戏玩法分析IV
    游戏玩法分析 selectround(avg(a.event_dateisnotnull),2)asfractionfrom(selectplayer_id,min(event_date)asevent_datefromactivitygroupbyplayer_id)aspleftjoinactivityasaonp.player_id=a.player_idanda.event_date=date_......
  • API接口item_get-获取lazada商品详情(num_iid宝贝ID、title商品标题、price价格、nick
    什么是API?API是一个缩写,它代表了一个pplicationPAGC软件覆盖整个房间。API是用于构建软件应用程序的一组例程,协议和工具。API指定一个软件程序应如何与其他软件程序进行交互。例行程序:执行特定任务的程序。例程也称为过程,函数或子例程。协议:在两个系统之间传输数据的格式。......
  • 【DP】LeetCode 221. 最大正方形
    题目链接221.最大正方形思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums以前i个元素组成(即nums[i-1])的状态;dp[i][j]分别表示以nums1前i个元素(即nu......
  • LeetCode 周赛 342(2023/04/23)容斥原理、计数排序、滑动窗口、子数组 GCB
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。前天刚举办2023年力扣杯个人SOLO赛,昨天周赛就出了一场Easy-Easy-Medium-Medium的水场,不得不说LeetCode是懂礼数的......
  • leetcode343. 整数拆分
    classSolution{public:intf[60];//f[i]记录i能拆出的最大乘积intintegerBreak(intn){for(inti=2;i<=n;i++)for(intj=1;j<i;j++)//枚举最后一个拆出的数字,这里不能只循环到i/2f[i]=max(f[i],max(j*f[i-j],j*(i-j)));......
  • ASCII码图
    ASCII(AmericanStandardCodeforInformationInterchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC646。作者:sunsky303......
  • 储物柜语音方案设计,NV040C的应用
    智能储物柜又称之为自动存包柜、电子寄存柜、电子储物柜等,在我们日常生活中可以帮助购物者或娱乐休闲的人们保证财产的安全。智能储物柜已广泛应用于超市、百货店、学校、图书馆、娱乐场所、工厂、机关、医院、电影城、游泳馆、海滨浴场、地铁站、火车站、机场等一切公共场所。而......
  • leetcode377.组合总和IV
    classSolution{public:longlongf[1010];//f[i]表示总和为i的选法个数intcombinationSum4(vector<int>&nums,inttarget){intn=nums.size();f[0]=1;for(inti=0;i<=target;i++)for(intj=0;j<n;j++)......
  • Prime k-tuple UVA - 1404
        一个步骤: 在[a,b]中标记S的倍数 #include<iostream>#include<cstring>#include<algorithm>#include<vector>usingnamespacestd;constintN=1e5+20;constintQ=2e9+2;#defineintlonglongboolvis[Q];intb[N],pm[N],tot=0;......
  • 请求处理类 yii\web\Request
    $request=Yii::$app->request;//请求对象//$request->enableCsrfValidation=false;//取消CSRF验证$resolve=$request->resolve();//请求拆分$getHeaders=$request->getHeaders();//请求头集合$getMethod=$request->getMethod(......