【Combination Sum 】cpp

题目:

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 (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3]

代码:

复制代码
class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
            vector<vector<int> > ret;
            vector<int> tmp; 
            int sum = 0;
            std::sort(candidates.begin(), candidates.end());
            Solution::dfs(ret, tmp, sum, candidates, 0, candidates.size()-1, target);
            return ret;
    }
    static void dfs( 
            vector<vector<int> >& ret, 
            vector<int>& tmp, 
            int &sum,
            vector<int>& candidates, 
            int begin, 
            int end, 
            int target )
    {
            if ( sum>target ) return;
            if ( sum==target )
            {
                ret.push_back(tmp);
                return;
            }
            for ( int i=begin; i<=end; ++i )
            {
                if ( sum+candidates[i]<=target )
                {
                    sum += candidates[i];
                    tmp.push_back(candidates[i]);
                    Solution::dfs(ret, tmp, sum, candidates, i, end, target);
                    sum -= candidates[i];
                    tmp.pop_back();
                }
            }
    }
};
复制代码

tips:

采用深搜模板:

1. 终止条件sum>target

2. 加入解集条件sum==target

3. 遍历当前层所有分支(如果满足sum+candidates[i]<target,则还可以再往上加candidates[i];注意,这里传入下一层的begin下标为i,因为要求元素可以无限多重复)

4. 由于传入下一层始终满足begin<=end,因此不要在终止条件中加入(begin>end)

=======================================

第二次过这道题,用dfs的思路,一次AC了。

复制代码
class Solution {
public:
        vector<vector<int> > combinationSum(
            vector<int>& candidates, 
            int target)
        {
            vector<vector<int> > ret;
            vector<int> tmp;
            sort(candidates.begin(), candidates.end());
            Solution::dfs(ret, tmp, candidates, 0, candidates.size()-1, target);
            return ret;
        }
        static void dfs(
            vector<vector<int> >& ret,
            vector<int>& tmp,
            vector<int>& candidates,
            int begin,
            int end,
            int target
            )
        {
            if ( target<0 ) return;
            if ( target==0 )
            {
                ret.push_back(tmp);
                return;
            }
            for ( int i=begin; i<=end; ++i )
            {
                tmp.push_back(candidates[i]);
                Solution::dfs(ret, tmp, candidates, i, end, target-candidates[i]);
                tmp.pop_back();
            }
        }
};
复制代码

 

posted on   承续缘  阅读(248)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示