Combination Sum算法详解

来源:互联网 发布:淘宝代刷灰烬套装 编辑:程序博客网 时间:2024/05/16 09:00

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

题意:给定一个正数集合和一个目标数,求出所有的子集合,使子集的和为target,要求子集合递增排列,且不包含重复的子集合,子集合中的数字可以重复

思路:先排序,递归求所有数字的集合,并判断数是否等于target

    void CombinationSumCore(const vector<int>& candidates,vector<vector<int>>& res,vector<int>& com,int target,int pos)    {        if(target==0)        {            res.push_back(com);            return;        }        for(int i=pos;i<candidates.size();i++)        {            if(candidates[i]>target)break;            com.push_back(candidates[i]);            CombinationSumCore(candidates,res,com,target-candidates[i],i);            com.pop_back();        }    }    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {        sort(candidates.begin(),candidates.end());        vector<vector<int>> res;        vector<int> com;        CombinationSumCore(candidates,res,com,target,0);        return res;    }

Combination Sum 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 (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 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]

题意:给定一个正数集合和一个目标数,求出所有的子集合,使子集的和为target,要求子集合递增排列,且不包含重复的子集合,原集合中的数字在每个子集中最多出现1次

思路:先排序,递归求所有数字的子集合,并判断数是否等于target

    void CombinationSumCore(const vector<int>& candidates,vector<vector<int>>& res,vector<int>& com,int target,int pos)    {        if(target==0)        {            res.push_back(com);            return;        }        for(int i=pos;i<candidates.size();i++)        {            if(candidates[i]>target)break;            if(i==pos||candidates[i]!=candidates[i-1])            {                com.push_back(candidates[i]);                CombinationSumCore(candidates,res,com,target-candidates[i],i+1);                com.pop_back();            }        }    }    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {        sort(candidates.begin(),candidates.end());        vector<vector<int>> res;        vector<int> com;        CombinationSumCore(candidates,res,com,target,0);        return res;    }

Combination Sum III:
题目:Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

题意:给定1~9这9个数字,以及k,n,求出所有的子集合,使子集的和为n,要求子集合的大小为k,递增排列,且不包含重复的子集合,原集合中的1~9数字每个数字最多出现1次

思路:递归求所有数字的子集合,并判断数是否等于target

    void CombinationSumCore(vector<vector<int>>& res,vector<int>& com,int target,int pos,int k)    {        if(target==0)        {            if(com.size()==k)res.push_back(com);            return;        }        for(int i=pos;i<=9;i++)        {            if(i>target)break;            com.push_back(i);            CombinationSumCore(res,com,target-i,i+1,k);            com.pop_back();        }    }    vector<vector<int>> combinationSum3(int k, int n) {        vector<vector<int>> res;        vector<int> com;        CombinationSumCore(res,com,n,1,k);        return res;    }
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肝内胆管结石疼怎么办 肝内胆管有结石怎么办 肝里胆管有结石怎么办 被信任的人算计你怎么办 卡罗拉1.8油耗高怎么办 请问09年途锐柴油版怎么办 油电混合没电了怎么办 前向运动精子3%怎么办 精子活力正常精子活率低怎么办? 精子形态正常率低怎么办 前向运动精子20%怎么办 不运动精子率高怎么办 前向运动精子少怎么办 精子前向运动力低怎么办 精子异常形态率高怎么办 精子正常率才1%怎么办 精子头部缺陷率高怎么办 前向运动精子15%怎么办 精子向前运动力低怎么办 前向运动精子10%怎么办 前向运动精子12%怎么办 前向运动精子2%怎么办 正常精子形态只有10%怎么办 精子混合畸形率高怎么办 正常形态精子才2怎么办 精子密度低至0.8怎么办 精子总活动率低怎么办 前向运动精子低怎么办 精子畸形率百分之94怎么办 实验室授权签字人考不过怎么办 万和热水器排污口漏水怎么办 万和热水器水箱漏水怎么办 军训鞋大了怎么办妙招 麽稍神经不好受怎么办 绒面高跟鞋太硬怎么办 新买的鞋子太硬怎么办 鞋底太硬脚掌疼怎么办 耐克鞋子走路吱吱响怎么办 两只鞋子有色差怎么办 劳保鞋鞋底太硬怎么办 在学校校服丢了怎么办