39. Combination Sum
2016-03-05 18:17
246 查看
1. 每个元素可用多次
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:
2. 每个元素只能用一次
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
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:
if(i > start && candidates[i] == candidates[i-1])continue;中的i > start可以保证没有重复结果。
3. 数字范围1-9,给定个数。
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:
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]
void cal(vector<int>& candidates, int start, int target, vector<int>& v, vector<vector<int>>& ans) { if(0 == target) { ans.push_back(v); return; } if(target < 0) return; int n = candidates.size(), i; for(i = start; i < n; i++) { if(i > start && candidates[i] == candidates[i-1]) continue; v.push_back(candidates[i]); cal(candidates, i, target-candidates[i], v, ans); v.pop_back(); } } vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> ans; vector<int> v; sort(candidates.begin(), candidates.end()); cal(candidates, 0, target, v, ans); return ans; }
2. 每个元素只能用一次
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
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
10,1,2,7,6,1,5and target
8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
void cal(vector<int>& candidates, int start, int target, vector<int>& v, vector<vector<int>>& ans) { if(0 == target) { ans.push_back(v); return; } if(target < 0) return; int n = candidates.size(), i; for(i = start; i < n; i++) { if(i > start && candidates[i] == candidates[i-1]) continue; v.push_back(candidates[i]); cal(candidates, i+1, target-candidates[i], v, ans); v.pop_back(); } } vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { vector<vector<int>> ans; vector<int> v; sort(candidates.begin(), candidates.end()); cal(candidates, 0, target, v, ans); return ans; }
if(i > start && candidates[i] == candidates[i-1])continue;中的i > start可以保证没有重复结果。
3. 数字范围1-9,给定个数。
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]]
void cal(vector<int>& candidates, int start, int target, int k, vector<int>& v, vector<vector<int>>& ans) { if(0 == target && 0 == k) { ans.push_back(v); return; } if(target < 0 || 0 == k) return; int n = candidates.size(), i; for(i = start; i < n; i++) { if(i > start && candidates[i] == candidates[i-1]) continue; v.push_back(candidates[i]); cal(candidates, i+1, target-candidates[i], k-1, v, ans); v.pop_back(); } } vector<vector<int>> combinationSum3(int k, int target) { vector<vector<int>> ans; vector<int> candidates, v; for(int i = 1; i <= 9; i++) candidates.push_back(i); cal(candidates, 0, target, k, v, ans); return ans; }
相关文章推荐
- 关于低功耗蓝牙的连接参数更新
- Spring核心IOC容器实现分析
- ViewPager禁止滑动以及它与内层滑动控件水平方向上事件冲突的解决方法
- 深入学习JavaScript的AngularJS框架中指令的使用方法
- 极路由 openwrt 使用 SyncY 实现百度云同步
- 初识Hash
- js
- 反转链表
- Android 实现全屏 去掉标题栏
- spring mvc使用Maven配置Velocity
- 第一次作业
- Java第一周学习总结5311
- 客户端与服务端Post报文构造请求及Http Post与Get请求方法
- IntentService
- JQuery之ajax
- AJAX - 创建 XMLHttpRequest 对象
- JSP九大内置对象和四种属性范围解读
- 编程技巧
- StringBuffer源码分析
- Java中接口式的匿名内部类的构造方法