• 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 (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]

    Code:

    class Solution {
    public:
        void findSum(vector<int> &S,int sum,int level,int &t,vector<int> &buf,vector<vector<int>> &res){
            for(int i=level;i<S.size();i++){
                buf.push_back(S[i]);
                if(sum+S[i]==t)
                    res.push_back(buf);
                if(sum+S[i]<=t)
                    findSum(S,sum+S[i],i,t,buf,res);
                buf.pop_back();
            }
        }
        vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
            vector<int> buf;
            vector<vector<int>> res;
            sort(candidates.begin(),candidates.end());
            findSum(candidates,0,0,target,buf,res);
            return res;
        }
    };
    

      

  • 相关阅读:
    select详解
    java Map及Map.Entry详解
    Java 基本类型
    java 获取String出现最多次数的字段
    java 居民身份证的校验
    java 删除文件
    Java 导出excel进行换行
    获取文件及其文件路径
    List<Map<String,Object>> 中文排序
    Java ----单个list 删除元素
  • 原文地址:https://www.cnblogs.com/winscoder/p/3535467.html
Copyright © 2020-2023  润新知