博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】Combination Sum (middle)
阅读量:6669 次
发布时间:2019-06-25

本文共 3224 字,大约阅读时间需要 10 分钟。

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
> combinationSum(vector
&candidates, int target) { vector
> ans; if(candidates.empty()) return ans; sort(candidates.begin(), candidates.end()); //从小到大排序 recursion(ans, candidates, 0 , target); return ans; } void recursion( vector
> &ans, vector
candidates, int k, int target) { static vector
partans; if(target == 0) //如果partans中数字的总和已经达到目标, 压入答案 { ans.push_back(partans); return; } if(target < 0) return; for(int i = k; i < candidates.size(); i++) //当前压入大于等于candidates[k]的数字 { int sum = candidates[i]; while(sum <= target) //数字可以压入多次,只要和小于等于目标即可 { partans.push_back(candidates[i]); recursion(ans, candidates, i + 1, target - sum); //后面只压入大于当前数字的数,避免重复 sum += candidates[i]; } while(!partans.empty() && partans.back() == candidates[i]) //状态还原 partans.pop_back(); } }};

 

其他人更短的递归,用参数来传partans. 省略了状态还原的代码,数字压入多次也采用了递归而不是循环

class Solution {public:    void search(vector
& num, int next, vector
& pSol, int target, vector
>& result) { if(target == 0) { result.push_back(pSol); return; } if(next == num.size() || target - num[next] < 0) return; pSol.push_back(num[next]); search(num, next, pSol, target - num[next], result); pSol.pop_back(); search(num, next + 1, pSol, target, result); } vector
> combinationSum(vector
&num, int target) { vector
> result; sort(num.begin(), num.end()); vector
pSol; search(num, 0, pSol, target, result); return result; }};

 

其他人动态规划的代码,还没看,速度并不快, 但很短

class Solution {public:    vector
> combinationSum(vector
&candidates, int target) { sort(candidates.begin(), candidates.end()); vector< vector< vector
> > combinations(target + 1, vector
>()); combinations[0].push_back(vector
()); for (auto& score : candidates) for (int j = score; j <= target; j++){ auto sls = combinations[j - score]; if (sls.size() > 0) { for (auto& s : sls) s.push_back(score); combinations[j].insert(combinations[j].end(), sls.begin(), sls.end()); } } return combinations[target];}};

 

转载地址:http://rvoxo.baihongyu.com/

你可能感兴趣的文章
netperf 而网络性能测量
查看>>
java反思reflect 分析Object物
查看>>
Java语法糖4:内部类
查看>>
android 消息推送
查看>>
Web Service Error wsse:InvalidSecurity Policy Requires Integrity (Doc ID 1370736.1)
查看>>
Android学习笔记之AndroidManifest.xml文件解析
查看>>
libpomelo 增加编译静态库cocos2d-x xcode 工程
查看>>
Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse
查看>>
Java系列:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path...
查看>>
AutoMapper(二)
查看>>
OpenGL ES 3.0之VertexAttributes,Vertex Arrays,and Buffer Objects(九)
查看>>
as3随机数
查看>>
四种方案解决ScrollView嵌套ListView问题
查看>>
[IIS] IIS Framework "aspnet_regiis.exe" 注册
查看>>
乘法逆元模板
查看>>
Oracle更改redo log大小 or 增加redo log组
查看>>
matplotlib油漆基础
查看>>
SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial
查看>>
为什么要优化你的代码?
查看>>
各类小公式
查看>>