leetcode-Combinations
2015-11-07 20:27
274 查看
Difficulty: Medium
Given two integers n and k, return all possible combinations of
k numbers out of 1 ... n.
If n = 4 and k = 2, a solution is:
Given two integers n and k, return all possible combinations of
k numbers out of 1 ... n.
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
class Solution { vector<vector<int> > res; void helper(vector<int> &base,int n,int left,int j){ if(left==0){ res.push_back(base); return ; } for(int i=j;i<=n;++i){ base.push_back(i); helper(base,n,left-1,i+1); base.pop_back(); } } public: vector<vector<int>> combine(int n, int k) { vector<int> base; helper(base,n,k,1); return res; } };
相关文章推荐
- Java根据模板创建excel文件
- HDU 4510 模拟
- dot 转换的一个小工具
- 安全的本质
- 例4-2 UVa489 Hangman Judge
- hdu-3062
- 开源框架open-replicator原理分析
- day11
- UFT中表的用法
- 链栈 多链栈
- 第五次作业
- Objective-C之用C的字符来处理NSString相关的字符替换和拼接的问题
- Android 适配器委托模式
- JNI中如何在本地代码C++中操作java中的基本类型的数组以及对象数组
- 了解 Linux內核架構 (2)
- 重新延时运行的Js 实现
- android移植pppoe拨号上网的全过程
- 【Alpha发布】贡献分分配
- 大道至简第六章阅读笔记
- 多栈共享