数字组合-LintCode
来源:互联网 发布:sniffer linux 编辑:程序博客网 时间:2024/06/16 20:55
给出一组候选数字(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T。C中的数字可以无限制重复被选取。
例如,给出候选数组[2,3,6,7]和目标数字7,所求的解为:
[7],
[2,2,3]
注意事项
所有的数字(包括目标数字)均为正整数。
元素组合(a1, a2, … , ak)必须是非降序(ie, a1 ≤ a2 ≤ … ≤ ak)。
解集不能包含重复的组合。
样例
给出候选数组[2,3,6,7]和目标数字7
返回 [[7],[2,2,3]]
#ifndef C135_H#define C135_H#include<iostream>#include<vector>#include<algorithm>using namespace std;class Solution {public: /** * @param candidates: A list of integers * @param target:An integer * @return: A list of lists of integers */ vector<vector<int> > combinationSum(vector<int> &candidates, int target) { // write your code here vector<vector<int>> v; vector<int> val; if (candidates.empty()) return v; sort(candidates.begin(), candidates.end()); candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end()); combinationSumRecur(candidates, val, v, 0, 0, target); return v; } void combinationSumRecur(vector<int> &candidates, vector<int> &val, vector<vector<int>> &v, int sum, int cur, int target) { if (sum == target) { v.push_back(val); return; } if (sum > target) return; for (int i = cur; i < candidates.size(); ++i) { val.push_back(candidates[i]); sum += candidates[i]; combinationSumRecur(candidates, val, v, sum, i, target); sum -= val.back(); val.pop_back(); } }};#endif
阅读全文
0 0
- LintCode-数字组合 II
- lintcode-数字组合-135
- LintCode:数字组合
- LintCode:数字组合 II
- lintcode ----数字组合
- LintCode: 数字组合 II
- LintCode:数字组合
- 数字组合II-lintcode
- lintcode--数字组合
- lintcode -- 数字组合II
- 数字组合-LintCode
- [Lintcode] #135 数字组合
- lintcode--数字组合Ⅱ
- lintcode-数字组合II-153
- [Lintcode] #153 数字组合 II
- LINTCODE——数字组合,数字组合II
- LintCode 135-数字组合 回溯法
- lintcode(153)数字组合 II
- CSU-ACM2017暑假集训2-二分搜索 D
- AndroidStudio 保存当前工作状态失败,重启后恢复为初始状态的解决方案
- Android:关于ContentProvider的知识都在这里了!
- python3基础-io
- 公务员面试之综合分析真题解析
- 数字组合-LintCode
- Can you solve this equation?--CSU-ACM2017暑假集训2-二分搜索
- 阿里云数据库专家玄惭的“武功”全记录之性能优化篇
- 【HDU】6038
- Spark2.x---3. RPC源码解析
- 第八届ACM校赛->魔戒
- 搜索关键字拼音智能提示实现
- 1959简单枚举类型——植物与颜色
- js实现首尾相连的文字滚动效果