39. Combination Sum I/II

基础backtracing题,先排序一下,每次传一个参数表示开始的下标。

class Solution {
public:
    vector<vector<int>> res;

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        sort(candidates.begin(),candidates.end());
        vector<int> tmp;
        dfs(candidates, target, tmp, 0);
        return res;
    }

    void dfs(vector<int>& candidates, int target, vector<int> &tmp, int start){
        if (target<0) return;
        if (target==0) res.push_back(tmp);
        for (int i=start;i<candidates.size();++i){
            tmp.push_back(candidates[i]);
            dfs(candidates,target-candidates[i],tmp,i);
            tmp.pop_back();
        }
    }
};

40. Combination Sum II

有重复元素的情况。和Permutation II处理方法类似,如果 i>start && candidates[i] == candidates [i-1] , 说明当前元素是这层dfs的重复元素,不需要再对candidates[i]进行递归了。

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> cur;
        sort(candidates.begin(),candidates.end());
        dfs(candidates,target,res,cur,0);
        return res;
    }

    void dfs(vector<int>& candidates, int target, vector<vector<int>> &res, vector<int> &cur, int start){
        if (target<0) return;
        if (target==0) res.push_back(cur);
        for (int i=start;i<candidates.size();++i){
            if (i>start && candidates[i]==candidates[i-1]) continue;
            cur.push_back(candidates[i]);
            dfs(candidates,target-candidates[i],res,cur,i+1);
            cur.pop_back();
        }
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/9602329.html

时间: 2024-10-12 13:30:01

39. Combination Sum I/II的相关文章

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

刷题39. Combination Sum

一.题目说明 题目39. Combination Sum,是从正数列表中选取几个,其和等于目标数的可能组合.任何一个数可以重复取,如candidates = [2,3,6,7], target = 7,结果集合是[ [7], [2,2,3] ] 如candidates = [2,3,5], target = 8,结果集合是 [ [2,2,2,2], [2,3,3], [3,5] ] 题目难度是Medium,先思考一下,再来解答. 二.我的解答 经过一番思考,这个题目可以通过dfs(树的深度优先遍

Leetcode 39 40 216 Combination Sum I II III

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一篇文章的思路略作改进. 例 1,求元素数量为定值的所有子集 Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n 

【LeetCode】Combination Sum I &amp; II 解题报告

[Combination Sum I] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

LeetCode Solutions : Combination Sum I &amp; II

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

[Leetcode][Python]39: Combination Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 39: Combination Sumhttps://oj.leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi