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>> res;
int myn;
int num;
public:
void tra(int loc,int start,vector<int> temp){
if(loc==num+)
{
res.push_back(temp);
return;
}
for (int i=start;i<=myn;++i)
{
temp.push_back(i);
tra(loc+,i+,temp);//不是start+1,而是i+1
temp.erase(temp.end()-);
}
}
vector<vector<int>> combine(int n, int k) {
myn=n;
num=k;
vector<int> temp;
tra(,,temp);
return res;
}
};

  注释处是i+1,原因在于

												

Combinations(带for循环的DFS)的更多相关文章

  1. Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  2. 【转】Android开发实践:自定义带消息循环(Looper)的工作线程

    http://ticktick.blog.51cto.com/823160/1565272 上一篇文章提到了Android系统的UI线程是一种带消息循环(Looper)机制的线程,同时Android也 ...

  3. LeetCode Letter Combinations of a Phone Number (DFS)

    题意 Given a digit string, return all possible letter combinations that the number could represent. A ...

  4. 在不开启事件循环的线程中使用QTimer(QThread::run函数自带事件循环,在构造函数里创建线程,是一种很有意思的线程用法) good

    引入 QTimer是Qt自带的定时器类,QTimer运行时是依赖于事件循环的,简单来说,在一个不开启事件循环(未调用exec() )的线程中,QTimer是无法使用的.通过分析Qt源码可发现,调用QT ...

  5. ping IP 带时间戳循环显示并写入日志(windos版+linux版)

    在工作中,判断网络是否通畅,首选命令就是ping,但有时候我们需要持续ping一个或多个地址时,需要加 -t 即可,但有时候需要在ping的时候加入时间戳并把ping记录写入到日志里面,方法如下: w ...

  6. Word Search(深度搜索DFS,参考)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. angular.foreach 循环方法使用指南

    angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” },{a:}]; angular.forEach ...

  8. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  9. bzoj 1574: [Usaco2009 Jan]地震损坏Damage【dfs】

    和March的那道不一样,只是非常单纯的带着贪心的dfs 首先一个点被隔断,与它相邻的所有点也会被隔断,打上删除标记,从1dfs即可 #include<iostream> #include ...

随机推荐

  1. css3中content属性的应用

    可以使用css3中content功能为html元素增减内容.content需要配合 E:before和E:after使用. 废话少说,看代码和效果说明: 第一种: css代码: #div1:befor ...

  2. eclipse设置Tomcat超级详细

    刚接触Ajax,创建了jsp文件发现错误 必备软件:tomcat(从apache的官方网站上下载一个,我的是apache-tomcat-8.0.28) 需要下载tomcatPluginV321.zip ...

  3. 解决国内无法安装android sdk的问题

    在使用 Android SDK Manager 的时候,主要会连接到两个地址 dl.google.com 和 dl-ssl.google.com,key发现这两个地址都是无法正常访问的,如何解决呢? ...

  4. Zookeeper系列(一)

    一.ZooKeeper的背景 1.1 认识ZooKeeper ZooKeeper---译名为“动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而 ...

  5. oracle 表之间的连接

    排序 - - 合并连接(Sort Merge Join, SMJ): a) 对于非等值连接,这种连接方式的效率是比较高的. b) 如果在关联的列上都有索引,效果更好. c) 对于将2个较大的row s ...

  6. linux 服务脚本

    #!/bin/bash # # chkconfig: # description: my_SERVICE_NAME is a my Service # # common function . /etc ...

  7. BZOJ 2039 人员雇佣 二元关系 最小割

    题面太长了,请各位自行品尝—>人员雇佣 分析: 借用题解的描述: a.选择每个人有一个代价Ai b.如果有两个人同时选择就可以获得收益Ei,j c.如果一个人选择另一个不选会产生代价Ei,j 这 ...

  8. Layui表格之动态添加数据(表格多选解决方案)

    前言: Layui已经给出了多选记录的解决方案,是在每条数据的前面加上CheckBox,每次选择都有监听.效果是这样: 实现监听的代码如下,这是一种解决选择多条数据的方案: table.on('edi ...

  9. MySQL 快速入门教程

    转:MySQL快速 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数据 ...

  10. 树莓派-3 启用root

    默认是user: pi,  password: raspberry 通过如下设置root密码并启用 pi@raspberrypi:~ $ sudo passwd root Enter new UNIX ...