LeetCode *** 39. Combination Sum
2016-04-15 21:42
323 查看
题目:
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
A solution set is:
分析:
睡了一觉感觉自己精神都好很多。。
代码:
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,7and target
7,
A solution set is:
[7]
[2, 2, 3]
分析:
睡了一觉感觉自己精神都好很多。。
代码:
class Solution { public: int tg; vector<vector<int>> res; vector<vector<int>> combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); tg=target; vector<int>tmp; dfs(candidates,tmp,0,0); return res; } void dfs(vector<int>& nums,vector<int> tmp,int start,int sum){ int s; for(int i=start;i<nums.size();i++){ s=sum+nums[i]; if(s<tg){ tmp.push_back(nums[i]); dfs(nums,tmp,i,s); tmp.erase(tmp.end()-1); }else if(s==tg){ tmp.push_back(nums[i]); res.push_back(tmp); tmp.erase(tmp.end()-1); } } } };
相关文章推荐
- 推理逻辑算法--------------问题抽象与数学建模
- 深入剖析Java中的装箱和拆箱
- MySQL 基础 —— DQL(数据查询)
- nyoj 756 重建二叉树(总结)
- 四种聚类算法的比较
- MVC、MVP、MVVM的区别
- tomcat内存设置
- java.util.ServiceLoader使用
- hdu 1233 还是畅通工程 最小生成树prim
- Asp.net 服务器控件
- java.util.ServiceLoader使用
- TCP三次握手以及释放连接
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- 面试题:单链表排序
- Android--Camera.addCallbackBuffer/setPreviewCallbackWithBuffer模式使用总结
- 32bit程序在64bit操作系统下处理重定向细节
- 初识树状数组 Educational Codeforces Round 10 D - Nested Segments
- 【NOIP practice】BSOJ1878 山头狙击战 二分
- 前20名的不安全密码(需要避免)
- OLLYDBG快捷键和页面布局