leetcode—— 39——Combination Sum
2016-04-16 16:00
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:
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: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> result; vector<int> path; sort(candidates.begin(),candidates.end()); helper(candidates,0,target,path,result); return result; } void helper(vector<int> &nums,int pos,int target,vector<int>& path,vector<vector<int>> & result) { if(0==target) { result.push_back(path); return ; } if(target < 0) return ; for(int i=pos;i<nums.size();i++) { path.push_back(nums[i]); helper(nums,i,target-nums[i],path,result); path.pop_back(); } } };
相关文章推荐
- Android——黑名单管理
- Android——事务
- 递归实现前向匹配分词
- 我的感想
- leetcode之largest number
- leetcode 19:Remove Nth Node From End of List
- 旋转字符串
- 对一个单链表进行逆序排列。
- 第二次_r日记管理
- 精灵王子逃出洞穴
- LINUX 下查看GPU信息
- 第一个安卓程序,传递数据
- web服务器搭建之百度BAE
- 【转】Java 项目UML反向工程转化工具
- iptables详解与实例
- Launcher3源码浅析(5.1)--Launcher.java
- 将一个单向链表逆序
- 基于canvas实现物理运动效果与动画效果(一)
- 【转】Java 项目UML反向工程转化工具
- Java程序编译和运行的过程