42_leetcode_Combinations
来源:互联网 发布:芒果tv最近新网络剧 编辑:程序博客网 时间:2024/04/29 19:09
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2,
1, 2; 1, 3; 1, 4; 2, 3; 2, 4; 3,4
1:特殊情况的考虑,2:采用递归的方式,首先选择当前的数字,之后在后面的数组中选择k-1;或者不选择当前的数字,而在数组后面选择k个数字;3:注意递归结束条件以及成功选择一组结束的条件
vector<vector<int> > combine(int n, int k) { vector<vector<int> > result; if(n <= 0 || k > n) { return result; } vector<int> temp; combineCore(n, k, 1, temp, result); return result; } void combineCore(int n, int k, int index, vector<int>& temp, vector<vector<int> >& result) { if(k == 0) { result.push_back(temp); return; } if(index > n) { return; } temp.push_back(index); combineCore(n, k - 1, index + 1, temp, result); temp.pop_back(); combineCore(n, k, index + 1, temp, result); }
0 0
- 42_leetcode_Combinations
- 42
- 42
- 42
- 42
- 42
- 42
- 练习42
- QStringListModel--42
- 42天
- nyoj-42
- 42 - Permutations
- spoj 42
- NYOJ 42
- 42 session
- NYOJ 42
- nyoj 42
- #job 42
- c#为什么读C Sharp??
- vc网络编程入门
- ubuntu下安装redis
- Linux 下安装配置 JDK7
- struts2 自定义验证器(身份证验证)
- 42_leetcode_Combinations
- iOS四种方法解析Jason数据
- ubuntu unity 3D桌面效果
- 有关windows Gateway Ipsec 和NAT 兼容性问题
- MFC实现全屏显示
- js实现复选框的操作-------Day41
- [LeetCode]—Gray Code 计算格雷码
- 串口转以太网(一)
- Vim 常用命令总结