Combinations
2015-08-18 14:13
274 查看
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, a solution is:
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
class Solution { public: void helper(vector<vector<int> > &svec, vector<int> &vec, int start, int n, int k) { if(vec.size() == k) { svec.push_back(vec); return; } for(int i = start; i < n; ++ i) { vec.push_back(i + 1); helper(svec, vec, i + 1, n, k); vec.pop_back(); } } vector<vector<int> > combine(int n, int k) { vector<vector<int> > svec; vector<int> vec; if(n == 0 || k == 0) { return svec; } helper(svec, vec, 0, n, k); return svec; } };
相关文章推荐
- php如何判断手机系统
- NSOperation的使用
- oracle读取文件
- 最短路径
- python读写文件,和设置文件的字符编码比如utf-8
- 如果忘记CitrixLicense Server的密码,造成无法登陆的话
- ffmpeg常用基本命令(转)
- HTML5 LocalStorage 本地存储
- 算法
- JAVA中的break[标签]continue[标签]用法
- Unix IPC之共享内存区(1)
- 最短路径
- Android四大组件之服务-Service 原理和应用开发详解
- oc 字典
- C#位操作与枚举的应用
- 证书的常见问题初探
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录
- iOS内存管理
- WaitForSingleObject()、WaitForMultipleObject() 函数
- Zabbix分布式监控系统使用总结