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,返回所有的k个数字组合,这些数字只能从1...n中选择~

分析:从cur == 0,cnt == 0开始,每次将cur + 1 ~ n之间的数字放入row中,并将cnt + 1,然后继续深度优先搜索直到cnt == k为止将row放入result中作为结果之一,不要忘记dfs遍历后还要将当前元素pop_back()出来,最后返回result~

class Solution {
public:vector<vector<int>> combine(int n, int k) {this->n = n, this->k = k;dfs(0, 0);return result;}
private:int n, k;vector<vector<int>> result;vector<int> row;void dfs(int cur, int cnt) {if (cnt == k) {result.push_back(row);return;}for (int i = cur + 1; i <= n; i++) {row.push_back(i);dfs(i, cnt + 1);row.pop_back();}}
};

LeetCode 77. Combinations相关推荐

  1. 【DFS】LeetCode 77. Combinations

    LeetCode 77. Combinations Solution1:我的答案 DFS,时间复杂度O(n!)O(n!)O(n!),空间复杂度O(n)O(n)O(n) class Solution { ...

  2. Leetcode 77. Combinations 组合

    Leetcode 77. Combinations 组合 标签 : Leetcode 题目地址: https://leetcode-cn.com/problems/combinations/ 题目描述 ...

  3. [leetcode] 77. Combinations @ python

    原题 Given two integers n and k, return all possible combinations of k numbers out of 1 - n. Example: ...

  4. LeetCode | 77. Combinations

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Exampl ...

  5. [LeetCode Python3]77. Combinations回溯

    77. Combinations class Solution:def __init__(self):self.res = []def trackback(self, track, index, n, ...

  6. LeetCode | 0077. Combinations组合【Python】

    LeetCode 0077. Combinations组合[Medium][Python][回溯] Problem LeetCode Given two integers n and k, retur ...

  7. leetcode 77. Combinations-排列|递归|非递归|Java|Python

    原题链接:77. Combinations [思路-Java.Python]递归实现 采用回溯算法.这是一道 NP 难问题,时间复杂度没办法提高,用一个循环递归处理子问题,问题的终止条件是每个组合中的 ...

  8. LeetCode.77. 组合

    LeetCode.77. 组合 难度:中等 这道题属于回溯的经典题目: 回溯模板: void backtracking(参数) {if (终止条件) {存放结果;return;}for (选择:本层集 ...

  9. 【LeetCode】77. Combinations 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

最新文章

  1. MAC终端密钥登录自动输入密码
  2. 作者赠送的《我的第一本c++书》收到啦
  3. 接口到底是什么(一语道破)
  4. MapReduce Java API实例-统计单词出现频率
  5. 恭喜 神策数据入选中国信通院开源供应商全景图
  6. mysql的innodb表生成的物理文件_MySQL innodb表使用表空间物理文件复制或迁移表
  7. 搭建(增加) jenkins slave 机器
  8. 怎样下载安装python_Windows系统上如何安装Python和pip
  9. shell中执行某条语句失败能不能重复执行_如何理解Mysql中的事务隔离级别?
  10. 大数据学习笔记50:Flume Channels(Flume通道)
  11. OpenWRT开源项目论坛遭未授权访问,可被用于供应链攻击
  12. php网站入门鹿泉银山,银山门传说与银山门古石洞的考证
  13. 西华大学c语言考试题,西华大学C语言程序设计复习题
  14. 报错:/check/src/check_log.c:27:10: 致命错误: subunit/child.h:没有那个文件或目录
  15. rpg游戏地图绘制android,基于Android studio 的rpg游戏大地图的绘制
  16. 一篇报告了解国内首个针对加密流量的检测引擎
  17. 【VS2017】VS2017离线安装教程
  18. Visual Studio 2015离线版msdn帮助文档下载和安装
  19. Tomcat下载——tomcat7、tomcat8、tomcat9官网下载链接
  20. java.lang.NoClassDefFoundError: com.android.tools.fd.runtime.AppInfo

热门文章

  1. Pow(x, n) 求一个数的n次方
  2. 1177:奇数单增序列
  3. 好程序员web分享图片标签、绝对路径和相对路径
  4. Python全栈开发之函数
  5. 对自己的python项目配置PYTHONPATH
  6. 剑指Offer(Java版):把字符串转换成整数
  7. 使用python标准库urllib2访问网页
  8. 安装Linux操作系统
  9. 专访McAfee“M双侠” 感受McAfee云安全自信
  10. 《Flask 入门教程》 第 2 章:Hello, Flask!