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: [
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
private:
vector<vector<int>> result;
vector<int> a ;
public:
void findResult(int n, int k, int num,int start )
{
if(num == k)
{
result.push_back(a);
return;
}
for(int i = start; i <= n; i++)
{ a[num] = i;
findResult( n, k, num+, i+); }
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function a.resize(k);
result.clear();
findResult( n, k, , ) ;
return result; }
};

重写后:

//Combinations
class Solution {
public:
void DFS(int n, int start,int k, vector<int> &ans){
if(ans.size() == k){
res.push_back(ans);
return;
}
for(int i = start; i <= n; i++)
{
ans.push_back(i);
DFS(n, i+, k, ans);
ans.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
vector<int> ans;
DFS(n, , k, ans);
return res; }
private:
vector<vector<int>> res;
};

最新文章

  1. JavaScript进阶之路(一)初学者的开始
  2. ubuntu中常用软件的安装
  3. Dockerfile完成Hadoop2.6的伪分布式搭建
  4. Flex Excel下载
  5. 关于JAVA多线程的那些事__初心者
  6. [转]epoll技术
  7. selenium2.0处理case实例(一)
  8. C51编译器扩展的关键词 &amp; C51中断函数的写法
  9. MC-设置 止盈
  10. 用Left join代替not in
  11. lua lua解读
  12. C语言可变參函数的实现
  13. 【原创】公司各个阶段 CTO 需要做什么?(上篇)
  14. Concept of function continuity in topology
  15. NOIAC41 最短路(线性基)
  16. Hexo:创建属于你自己的博客
  17. 使用 Docker 部署 Grafana + Prometheus 监控 MySQL 数据库
  18. LOJ-10105(欧拉回路模板,套圈法,递归)
  19. MySQL 安装 用户管理 常用命令
  20. list转化为json数组

热门文章

  1. Linux企业级项目实践之网络爬虫(6)——将程序设计成为守护进程
  2. SQL之用户自定义函数
  3. [VBA]发布一个计算桩号之差的Excel自定义函数(VBA)
  4. HTML--控制小人自由移动
  5. iPhone 5,6,6 plus 尺寸
  6. Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局
  7. Linux的目录结构及其作用
  8. python - 面向对象(一)
  9. Sqlserver in 实现 参数化查询 XML类型
  10. 在用EF新增对象存贮至数据库时汪报错,但数据库里没有新增数据