LeetCode 77. Combinations
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相关推荐
- 【DFS】LeetCode 77. Combinations
LeetCode 77. Combinations Solution1:我的答案 DFS,时间复杂度O(n!)O(n!)O(n!),空间复杂度O(n)O(n)O(n) class Solution { ...
- Leetcode 77. Combinations 组合
Leetcode 77. Combinations 组合 标签 : Leetcode 题目地址: https://leetcode-cn.com/problems/combinations/ 题目描述 ...
- [leetcode] 77. Combinations @ python
原题 Given two integers n and k, return all possible combinations of k numbers out of 1 - n. Example: ...
- LeetCode | 77. Combinations
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Exampl ...
- [LeetCode Python3]77. Combinations回溯
77. Combinations class Solution:def __init__(self):self.res = []def trackback(self, track, index, n, ...
- LeetCode | 0077. Combinations组合【Python】
LeetCode 0077. Combinations组合[Medium][Python][回溯] Problem LeetCode Given two integers n and k, retur ...
- leetcode 77. Combinations-排列|递归|非递归|Java|Python
原题链接:77. Combinations [思路-Java.Python]递归实现 采用回溯算法.这是一道 NP 难问题,时间复杂度没办法提高,用一个循环递归处理子问题,问题的终止条件是每个组合中的 ...
- LeetCode.77. 组合
LeetCode.77. 组合 难度:中等 这道题属于回溯的经典题目: 回溯模板: void backtracking(参数) {if (终止条件) {存放结果;return;}for (选择:本层集 ...
- 【LeetCode】77. Combinations 解题报告(Python C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
最新文章
- MAC终端密钥登录自动输入密码
- 作者赠送的《我的第一本c++书》收到啦
- 接口到底是什么(一语道破)
- MapReduce Java API实例-统计单词出现频率
- 恭喜 神策数据入选中国信通院开源供应商全景图
- mysql的innodb表生成的物理文件_MySQL innodb表使用表空间物理文件复制或迁移表
- 搭建(增加) jenkins slave 机器
- 怎样下载安装python_Windows系统上如何安装Python和pip
- shell中执行某条语句失败能不能重复执行_如何理解Mysql中的事务隔离级别?
- 大数据学习笔记50:Flume Channels(Flume通道)
- OpenWRT开源项目论坛遭未授权访问,可被用于供应链攻击
- php网站入门鹿泉银山,银山门传说与银山门古石洞的考证
- 西华大学c语言考试题,西华大学C语言程序设计复习题
- 报错:/check/src/check_log.c:27:10: 致命错误: subunit/child.h:没有那个文件或目录
- rpg游戏地图绘制android,基于Android studio 的rpg游戏大地图的绘制
- 一篇报告了解国内首个针对加密流量的检测引擎
- 【VS2017】VS2017离线安装教程
- Visual Studio 2015离线版msdn帮助文档下载和安装
- Tomcat下载——tomcat7、tomcat8、tomcat9官网下载链接
- java.lang.NoClassDefFoundError: com.android.tools.fd.runtime.AppInfo