Combination Sum
2015-11-25 21:57
239 查看
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:
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: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>>res; vector<int>tmp; sort(candidates.begin(),candidates.end()); helper(candidates,tmp,res,target,0); return res; } void helper(vector<int>&candidates,vector<int>tmp,vector<vector<int>>& res,int target,int start){ if(start>=candidates.size()) return; if(target==0){ res.push_back(tmp); return; } else if(target<0) return ; else{ helper(candidates,tmp,res,target,start+1); tmp.push_back(candidates[start]); helper(candidates,tmp,res,target-candidates[start],start); } } };
相关文章推荐
- JSON之生成JSON字符串和解析
- IOS学习——UIView Animation与CATransition
- 学习日志(1)
- Swift中的Reference Type和 Values Type
- Swift -5 面向对象基础(中)
- ubuntu如何完全卸载Java
- 直线生成算法——中点画线法(Middpoint)
- Struts2_Constant标签认识以及查文件源码和api文档[6]
- Socket 通信原理机制
- Android Studio打包.so文件教程
- 几个主流的Java连接池整理 (转) (2012-01-04 23:48:02)转载▼
- 快速上手php:使用PhpStrom部署项目
- ServletContext对象
- 115.Oracle数据库SQL开发之 PLSQL编程——循环
- MapReduce编程实例之自定义分区
- 交叉编译构建环境搭建
- ubuntu环境下安装R
- BZOJ 3916: [Baltic2014]friends( hash )
- Openstack学习历程_1_视频
- 泛型通配符