【一天一道LeetCode】#77. Combinations
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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],
]
(二)解题
本题大意:给定两个整形数,n和k,从1~n中找出所有k个数的组合。
这题是典型的回溯问题。分析一下当n=4,k=3的情况:
维持一个vector变量,当它的长度小于3时,按顺序往里面放数字。
第一个满足长度等于3的组合为1,2,3,接下来回溯弹出3,继续放数字4
第二个满足长度等于3的组合为1,2,4,然后回溯弹出4,没有数字放了,就继续回溯弹出2,再放入3和4
第三个满足长度等于3的组合为1,3,4,然后回溯弹出3,4和1,放入2,3,4
第四个满足长度等于3的组合为2,3,4。至此,算法结束。
class Solution {
public:
vector<vector<int>> ret;
vector<vector<int>> combine(int n, int k) {
vector<int> temp;
dfscombine(n,1,k,temp);
return ret;
}
void dfscombine(int n,int start,int k,vector<int> & temp)
{
if(temp.size()==k){//当长度为k时,将结果存在ret中
ret.push_back(temp);
return;
}
for(int i = start ; i <= n ; i++)
{
temp.push_back(i);//放入数字
dfscombine(n,i+1,k,temp);
temp.pop_back();//回溯到上一步
}
}
};
【一天一道LeetCode】#77. Combinations的更多相关文章
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Leetcode 77, Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- 【一天一道LeetCode】索引目录 ---C++实现
[一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...
- 【一天一道LeetCode】#93. Restore IP Addresses
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
随机推荐
- vs2017 +CUDA 9.0配置
环境: 1.Win7 64位 旗舰版 2.VS2017 3.CUDA 9.0 安装过程比较简单,直接运行在官网下载的CUDA安装包就可以了. 建议先安装VS,再安装CUDA.这样安装完之后会在VS里直 ...
- python学习之路网络编程篇(第一篇)socket初识
什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为socket.socket通常也称为“套接字”,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的 ...
- 原生Js写轮播图代码
html css js 在知道jQuery如何实现轮播效果的基础上,用js写代码 如图:标记这里的地方 理解一下 用到的知识: 1.HTML DOM 的appendChild() 和 removeCh ...
- Angular2的input和output(原先的properties和events)
angular2学习笔记 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 文章来源:http://blog.ng-book.com/angular-2 ...
- Programming In Scala笔记-第十七章、Scala中的集合类型
本章主要介绍Scala中的集合类型,主要包括:Array, ListBuffer, Arraybuffer, Set, Map和Tuple. 一.序列 序列类型的对象中包含多个按顺序排列好的元素,可以 ...
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava Retrofit,因为其简单与出色的性能,也是受到很多人的青睐,但是他和以往的通信 ...
- CRM 2013 Script Loading Deep Dive
关于CRM中脚本的加载次序梳理的很不错,可以看看 https://community.dynamics.com/crm/b/develop1/archive/2013/11/02/crm-2013-s ...
- 漏洞挖局利器-Fuzz技术介绍
模糊测试的定义 模糊测试定义为"通过向应用提供非预期的输入并监控输出中的异常来发现软件中的故障(faults)的方法". 典型而言,模糊测试利用自动化或是半自动化的方法重复地向应用 ...
- [tornado]使用webscoket的使用总是403错误
使用的tornado版本为4.0+ 后台: PS D:\CodeHouse\tornado\websocket> python .\ws_app.py WARNING:tornado.acces ...
- JAR、WAR、EAR的使用和区别
最近接触这几个词较多,停下来总结总结它们的区别和联系,更好的深刻理解 Jar.war.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压缩格式.但是它们的使用目的有所区别: ...