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. Java集合框架源码(四)——Vector

    第1部分 Vector介绍 Vector简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这 ...

  2. Python学习 Day 8 继承 多态 Type isinstance dir __slots__

    继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base clas ...

  3. js 日期时间大小比较

    <body> 开始时间:<input onfocus="setday(this)" id="startTime" name="sta ...

  4. Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库上下文

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库配置文件 下一篇:Farseer.net轻量级ORM开源 ...

  5. OpenFlow_tutorial_1_Introduce

    tutorial出处:https://github.com/mininet/openflow-tutorial/wiki OpenFlow是用于远程控制交换机流表(forwarding tables) ...

  6. tensorboard及summary data

    (新手上路,如果有不对的地方,望指正.另外有没有小伙伴一起学习交流啊?)   tensorboard为tensorflow提供了可视化,它的重要性不言而喻.   tensorboard是通过读取eve ...

  7. CREATE CONSTRAINT TRIGGER - 定义一个新的约束触发器

    SYNOPSIS CREATE CONSTRAINT TRIGGER name AFTER events ON tablename constraint attributes FOR EACH ROW ...

  8. 今天被 <!doctype html> 搞了两个小时,两个页面同样的样式,chosen右边的小箭头,一个上下居中对齐 一个居顶对齐。最后找到问题所在doctype

    今天被 <!doctype html> 搞了两个小时,两个页面同样的样式,chosen右边的小箭头,一个上下居中对齐 一个居顶对齐.最后找到问题所在doctype <-- 这个小箭头

  9. python msg_box

    转自:http://www.cnblogs.com/otfsenter/ # _*_ coding: utf-8 _*_ # @Time : 2017/3/27 17:39 # @Author : o ...

  10. [转]Delphi调用cmd并取得输出文本

    //转自http://www.oschina.net/code/snippet_136241_3980 1 procedure CheckResult(b: Boolean); begin if no ...