Combinations
来源:互联网 发布:大数据定义 编辑:程序博客网 时间:2024/04/29 21:39
Given two integers n and k, return all possible combinations ofk numbers out of 1 ...n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
分析:
DFS + 剪枝。
class Solution {public: vector<vector<int> > combine(int n, int k) { vector<int> path; vector<vector<int> > res; DFS(1, n, k, path, res); return res; }private: void DFS(int cur, int n, int k, vector<int>&path, vector<vector<int> >&res) { // exit if(k == 0){ res.push_back(path); return; } //only k elems left else if(n-cur+1 == k){ for(int i=cur; i<=n; ++i) path.push_back(i); res.push_back(path); path.erase(path.end()-k, path.end()); return; } //pick cur path.push_back(cur); DFS(cur+1, n, k-1, path, res); path.pop_back(); // do not pick cur DFS(cur+1, n, k, path, res); }};
1 0
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- combinations
- Combinations
- Combinations
- Combinations
- Combinations
- ZOJ 1003 Crashing Balloon
- C++:与字符串常量有关的几个重要概念string literal, string, C-style charater string
- arduino 8段数码管,arduino 8段数码管
- startactivtyforresult请求码与返回码的区别
- arduino蜂鸣器例子,arduino蜂鸣器例子
- Combinations
- arduino查看端口
- android 重力感应初步认识
- 在win7系统中如何修复错误无效的软件
- [eclipse]问题调试
- oracle数据的导入导出
- arduino电机驱动例子
- ,arduino 蓝牙例子,
- arduino巡线模块使用