【leetcode】Combination Sum I && III
2015-05-15 11:50
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 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
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]]
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: void com(vector<int> &candidates,int start,int sum,int target,vector<int> &path,vector<vector<int> > &res) { if(sum>target) return ; if(sum==target) { res.push_back(path); return ; } int len=candidates.size(); for(int i=start;i<len;i++) { path.push_back(candidates[i]); com(candidates,i,sum+candidates[i],target,path,res); path.pop_back(); } } vector<vector<int> > combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<vector<int> > res; vector<int> path; com(candidates,0,0,target,path,res); return res; } };
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:
class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<vector<int> >res; vector<int>item; dfs(k,n,1,item,res); return res; } void dfs(int k,int n,int level,vector<int> &item,vector<vector<int> >&res) { if(n<0) return ; if(n==0&&item.size()==k) res.push_back(item); for(int i=level;i<=9;i++) { item.push_back(i); dfs(k,n-i,i+1,item,res); item.pop_back(); } } };
[[1,2,6], [1,3,5], [2,3,4]]
相关文章推荐
- LeetCode: Combination Sum I && II && III
- 【LeetCode从零单刷】Combinations & Combination Sum 系列
- LeetCode : Best Time to Buy and Sell Stock III & IV
- leetcode--217&219&220 Contains Duplicate(重复)I&II&III
- 【LeetCode】Single Number I & II & III
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
- <LeetCode OJ>Single Number( I / II / III )【136 / 137 / 260】
- <LeetCode OJ> 216. Combination Sum III
- <LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)
- leetcode题解-151. Reverse Words in a String && 557. Reverse Words in a String III
- 【LeetCode】Best Time to Buy and Sell Stock I && II && III
- [LeetCode] Single Number & Single Number III
- leetcode Best Time to Buy and Sell Stock I&&II&&III
- [LeetCode]217. Contains Duplicate&219. Contains Duplicate II&220. Contains Duplicate III
- Leetcode 217. Contains Duplicate & 219. Contains Duplicate II & 220. Contains Duplicate III
- LeetCode - Best Time to Buy and Sell Stock I && II && III && IV
- leetcode笔记--Contains Duplicate I & II & III
- Leetcode解题笔记-Contains Duplicate && Contains Duplicate II&&Contain Duplicate III
- LeetCode:Single Number I & II & III
- leetcode-39&40&216 Combination Sum I&II&III