递归(2)
来源:互联网 发布:最优化方法 答案 编辑:程序博客网 时间:2024/06/03 19:58
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.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]
解析:此题主要运用递归来解决问题,以上周一题极为相似,因此一同做了。程序:
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target)
{
vector<vector<int>> res;
sort(num.begin(),num.end());
vector<int> local;
findCombination(res, 0, target, local, num);
return res;
}
void findCombination(vector<vector<int>>& res, const int order, const int target, vector<int>& local, const vector<int>& num)
{
if(target==0)
{
res.push_back(local);
return;
}
else
{
for(int i = order;i<num.size();i++) // iterative component
{
if(num[i]>target) return;
if(i&&num[i]==num[i-1]&&i>order) continue; // check duplicate combination
local.push_back(num[i]),
findCombination(res,i+1,target-num[i],local,num); // recursive componenet
local.pop_back();
}
}
}
};
- 递归 (枚举)2
- 递归(2)
- 递归算法--如何一步一步理解递归(2)--全排列
- 递归(学习笔记2)
- 数据结构学习-递归(2)
- 递归小论(2)
- caioj1032:递归2(组合)
- 1032: 递归2(组合)
- 递归2
- 离散数学之递归关系(2)
- Algorithm--全排列2递归(c#)
- 递归与分治策略(2)
- 10进制转2进制(递归)
- 递归(图的遍历)2
- 程序员面试宝典--8.2递归(2)
- 算法:递归总结(2)例子
- 用递归方法求解(2)
- 递归实战(2)字符串全排列
- opencv3+版本感觉还是不稳定啊
- 线程安全
- com.android.ddmlib.AdbCommandRejectedException: device offline
- Ubuntu更新源
- HTML中嵌入css的三种方式
- 递归(2)
- 线程的各种函数
- 使用pyinstaller过程中遇到的一些问题
- DirectFB学习之图形加速驱动的匹配
- 关于char, wchar_t, TCHAR, _T(),L,宏 _T、TEXT,_TEXT、L
- 实现弹性组件效果(贝塞尔曲线&&阻尼函数)
- 蓝桥杯比赛之嵌入式
- 数据结构专项练习(数组、链表、二叉树)
- bzoj1068[SCOI2007]压缩