首页 > 其他分享 >40. 组合总和 II(leetcode)

40. 组合总和 II(leetcode)

时间:2024-05-16 12:52:46浏览次数:16  
标签:target int sum 40 II start candidates path leetcode

https://leetcode.cn/problems/combination-sum-ii/description/

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    int sum = 0;
    
    public List<List<Integer>> combinationSum2(int[] candidates,int target) {
        //为了将重复的数字都放到一起,所以先进行排序
        Arrays.sort( candidates );
        backTracking( candidates, target, 0 );
        return res;
    }
    
    private void dfs(int[] candidates,int target,int start) {
        if(sum == target) {
            res.add( new ArrayList<>( path ) );
            return;
        }
        for( int i = start; i < candidates.length && sum + candidates[i] <= target; i++ ) {
            //正确剔除重复解的办法
            //跳过同一树层使用过的元素,因为前一次搜索已经搜索完以这个元素开头的组合了
            if ( i > start && candidates[i] == candidates[i - 1] ) {
                continue;
            }
            sum += candidates[i];
            path.add(candidates[i]);
            // i+1 代表当前组内元素只选取一次
            dfs(candidates,target,i + 1);
            int temp = path.getLast();
            sum -= temp;
            path.removeLast();
        }
    }
}

 

标签:target,int,sum,40,II,start,candidates,path,leetcode
From: https://www.cnblogs.com/lxl-233/p/18195771

相关文章

  • 抽象代数课程笔记 III —— 域论、伽罗瓦理论
    持续更新。\(\newcommand{\a}{\alpha}\newcommand{\b}{\beta}\newcommand{\D}{\Delta}\newcommand{\eps}{\varepsilon}\newcommand{\ph}{\varphi}\newcommand{\t}{\theta}\newcommand{\la}{\lambda}\newcommand{\si}{\sigma}\newcommand{\d}{......
  • 抽象代数课程笔记 III —— 域论、伽罗瓦理论
    持续更新。\(\newcommand{\a}{\alpha}\newcommand{\b}{\beta}\newcommand{\D}{\Delta}\newcommand{\eps}{\varepsilon}\newcommand{\ph}{\varphi}\newcommand{\t}{\theta}\newcommand{\la}{\lambda}\newcommand{\si}{\sigma}\newcommand{\d}{......
  • LeetCode 1669. Merge In Between Linked Lists
    原题链接在这里:https://leetcode.com/problems/merge-in-between-linked-lists/description/题目:Youaregiventwolinkedlists: list1 and list2 ofsizes n and m respectively.Remove list1'snodesfromthe ath nodetothe bth node,andput list2 in......
  • LeetCode 2595. Number of Even and Odd Bits
    原题链接在这里:https://leetcode.com/problems/number-of-even-and-odd-bits/description/题目:Youaregivena positive integer n.Let even denotethenumberofevenindicesinthebinaryrepresentationof n (0-indexed)withvalue 1.Let odd denotethen......
  • 解决新浪微博图床 403 批量下载图片等资源(以 MMChat 数据集为例)
    目录1.代码2.举一反三1.代码该Python脚本可多线程地批量下载新浪图床图片,每次下载会检查哪些图片已下载并过滤已下载的图片。importosimportrequestsfromconcurrent.futuresimportThreadPoolExecutor,as_completedimportloggingimporttimefromtqdmimport......
  • 39. 组合总和(leetcode)
    https://leetcode.cn/problems/combination-sum/description/两种搜索思路一种是选或不选,搜索树是一颗二叉树另一种是选哪个数,是一个n叉树二叉classSolution{List<List<Integer>>res=newArrayList<>();inttarget;int[]nums;List<Integer>......
  • 代码随想录算法训练营第第八天 | 344.反转字符串 、541. 反转字符串II、卡码网:54.替
    344.反转字符串建议:本题是字符串基础题目,就是考察reverse函数的实现,同时也明确一下平时刷题什么时候用库函数,什么时候不用库函数题目链接/文章讲解/视频讲解:https://programmercarl.com/0344.反转字符串.html/***@param{character[]}s*@return{void}Donotret......
  • 264 ugly number II 丑数
    问题描述Anuglynumberisapositiveintegerwhoseprimefactorsarelimitedto2,3,and5.Givenanintegern,returnthenth*uglynumber*.解释:一个丑数是一个正整数,其公因子只有2,3,5。给定数字n,求第n个丑数案例:Input:n=10Output:12Explanation:[1,2,......
  • 108. 将有序数组转换为二叉搜索树(leetcode)
    https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/要点是分割左右区间,并且分割时需要注意left和right相加可能会超过int,但是本题不需要classSolution{publicTreeNodesortedArrayToBST(int[]nums){//有返回值写法......
  • P1140 相似基因
    链接:https://www.luogu.com.cn/problem/P1140题目:思路:设置递推状态:dp[i][j]表示a的前i个碱基和b的前j个碱基配对的最大值。那么递推:1.ans1设置为dp[i-1][j-1]+val[a[i]][b[j]]就是说a[i]和b[j]可以凑一对,那么就凑;2.ans2设置为dp[i-1][j]+val[0][a[i]]就是说a[i]和b的空凑一......