Combinations(带for循环的DFS)
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)的更多相关文章
- Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【转】Android开发实践:自定义带消息循环(Looper)的工作线程
http://ticktick.blog.51cto.com/823160/1565272 上一篇文章提到了Android系统的UI线程是一种带消息循环(Looper)机制的线程,同时Android也 ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- 在不开启事件循环的线程中使用QTimer(QThread::run函数自带事件循环,在构造函数里创建线程,是一种很有意思的线程用法) good
引入 QTimer是Qt自带的定时器类,QTimer运行时是依赖于事件循环的,简单来说,在一个不开启事件循环(未调用exec() )的线程中,QTimer是无法使用的.通过分析Qt源码可发现,调用QT ...
- ping IP 带时间戳循环显示并写入日志(windos版+linux版)
在工作中,判断网络是否通畅,首选命令就是ping,但有时候我们需要持续ping一个或多个地址时,需要加 -t 即可,但有时候需要在ping的时候加入时间戳并把ping记录写入到日志里面,方法如下: w ...
- 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 ...
- angular.foreach 循环方法使用指南
angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” },{a:}]; angular.forEach ...
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- bzoj 1574: [Usaco2009 Jan]地震损坏Damage【dfs】
和March的那道不一样,只是非常单纯的带着贪心的dfs 首先一个点被隔断,与它相邻的所有点也会被隔断,打上删除标记,从1dfs即可 #include<iostream> #include ...
随机推荐
- spring 整合struts
1.例子:未被spring整合 struts.xml 的配置文件 <constant name="struts.enable.DynamicMethodInvocation" ...
- spark测试脚本-笔记
1)Spark配置&启动脚本分析 http://www.cnblogs.com/riordon/p/5732208.html
- windows 安装绿色版mysql
(1)到官网下载绿色版mysql:http://dev.mysql.com/downloads/mysql/ (2)下载好后,放在F:\mysql,解压出来 (3)进入到mysql-5.6.19-wi ...
- win+r 快速命令
control keymgr.dll 打开凭据管理器 secpol.msc 本地安全策略 mstsc 远程 msconfig 启动选项 %temp% 临时文件夹 \\192.168 ...
- win8怎么打开或关闭快速启动(进入BIOS前的设置)
win8系统之后,系统添加了快速启动功能,这让Windows的启动速度快了不少.但是,任何事物有利有弊,相信不少人在进入BIOS或者重装系统时遇到了麻烦.接下来我们看看在win8及以上版本怎么打开或关 ...
- swal用法
swal({ title: "确认删除?", text: "Your will not be able to recover this imaginary fil ...
- flask的基本搭建
from flask import Flask app = Flask(__name__) @app.route("/")def index(): return "ok& ...
- 【Linux】CentOS tar压缩与解压命令大全
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...
- 6-Java-C(无穷分数)
题目描述: 无穷的分数,有时会趋向于固定的数字. 请计算[图1.jpg]所示的无穷分数,要求四舍五入,精确到小数点后5位,小数位不足的补0. 请填写该浮点数,不能填写任何多余的内容. 正确算法: 此题 ...
- vue vueRouter vuex Axios webpack 前端常用内容
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.