• 【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();
                }
            }
    };
  • 相关阅读:
    [WPF]搜索列表项的四种实现方案点评
    一个生成均匀分布随机数的问题
    制作NSIS命令行窗口输出插件
    公司的Principle给出的高性能数据库设计,总觉得别扭
    用WordPress一步步建立自己的博客站点
    译作要严格尊重原著吗?——读〈Windows核心编程〉小感
    这么好的书怎么全国都缺货?
    【C语言】用c语言实现简单的五子棋
    【C语言】游戏菜单界面设计 与 游戏整合
    【C语言】基于五子棋框架上的 象棋 小游戏
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4535900.html
Copyright © 2020-2023  润新知