zoukankan      html  css  js  c++  java
  • Combination Sum I&&II(经典的回溯算法题)

    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 (including target) will be positive integers.
    • Elements in a combination (a1a2, … , 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 {
    private:
        vector<vector<int>> res;
        vector<int> temp;
        int tempsum=0;
       
        
    public:
        void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum)
        {
            
          if(initer==candidates.end()||tempsum>target)
                return ;
          if(tempsum==target)
          {
               // temp.push_back(*initer);
                res.push_back(temp);
                return ;
          }
           
          for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++)
          {
                temp.push_back(*iter);
                combinationSum(candidates,target,iter, tempsum+*iter);
                temp.pop_back();
               
         }
        }
        vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
            sort(candidates.begin(),candidates.end());
            vector<int>::iterator initer=candidates.begin();
            combinationSum(candidates,target,initer,0);
            return res;
            
            
        }
    };

     II:

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

    Each number in C may only be used once in the combination.

    Note:

    • All numbers (including target) will be positive integers.
    • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
    • The solution set must not contain duplicate combinations.

    For example, given candidate set 10,1,2,7,6,1,5 and target 8
    A solution set is: 
    [1, 7] 
    [1, 2, 5] 
    [2, 6] 
    [1, 1, 6]

    感觉自己代码写的有点复杂,不过好歹是通过了,接下来需要慢慢的把代码写简洁点,通用点。

    class Solution {
    private:
        vector<vector<int>> res;
        vector<int> temp;
    public:
        void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum)
        {
          if(tempsum==target)
          {
                if(find(res.begin(),res.end(),temp)==res.end())
                    res.push_back(temp);
                return ;
          }
          if(initer==candidates.end()||tempsum>target)
                return ;
          for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++)
          {
                temp.push_back(*iter);
                combinationSum(candidates,target,iter+1, tempsum+*iter);
                temp.pop_back();
               
         }
        }
        vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
            sort(candidates.begin(),candidates.end());
            vector<int>::iterator initer=candidates.begin();
            combinationSum(candidates,target,initer,0);
            return res;
            
            
        }
    };
      
  • 相关阅读:
    C语言常用函数-ispunct()判断字符是否为标点符号函数
    C语言常用函数-isupper()判断字符是否为大写英文字母函数
    C语言常用函数-isprint()判断字符是否为可打印字符(含空格)函数
    C语言常用函数-islower()判断字符是否为小写英文字母函数
    C语言常用函数-isgraph()判断字符是否为除空格外的可打印字符函数
    get请求多参数循环拼接
    数据库字段转驼峰命名
    React钩子函数问题,解决父组件实时更新子组件的值
    关于react 父级组件更新数据触发子组件更新渲染问题
    Jquery的ajax 方法
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4513021.html
Copyright © 2011-2022 走看看