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], ]
class Solution {public List<List<Integer>> combine(int n, int k) {List<List<Integer>> list = new ArrayList<>();if(n<k)return list;if(n==k){List<Integer> list1 = new ArrayList<>();for(int i=1;i<=n;i++)list1.add(i);list.add(list1);return list;}if(k == 1){for(int i=0;i<n;i++){List<Integer> list1 = new ArrayList<>();list1.add(i+1);list.add(list1);}return list;}List<List<Integer>> list2 = combine(n-1,k-1);for(List<Integer> l : list2){l.add(n);}list = combine(n-1,k);list.addAll(list2);return list;}
}
class Solution {public List<List<Integer>> combine(int n, int k) {List<List<Integer>> list = new ArrayList<>();if(n<k)return list;combine(list,new ArrayList<Integer>(),k,n,1);return list;}public void combine(List<List<Integer>> list,List<Integer> tempList,int k,int n,int start){if(tempList.size() == k){list.add(new ArrayList<Integer>(tempList));}else{for(int i=start;i<=n;i++){tempList.add(i);combine(list,tempList,k,n,i+1);tempList.remove(tempList.size()-1);}}}
}
上述2种解法中,递归解法在LeetCode上超越了90%左右,而使用回溯的解法只超越了40%多,显然递归要好一些。对此,我觉得可能是使用递归的过程中计算的都是我们需要求的组合,但是在回溯中会多次出现碰到“死胡同”这样的情况,导致了2种解法在时间空间上存在差异。举个例子:
if(tempList.size()+n-start+1 < k)return;
假设之后所有元素都加入tempList中都比k小的时候,我们不需要再计算了。
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 Python3]77. Combinations回溯
77. Combinations class Solution:def __init__(self):self.res = []def trackback(self, track, index, n, ...
- Leetcode 77. Combinations 组合
Leetcode 77. Combinations 组合 标签 : Leetcode 题目地址: https://leetcode-cn.com/problems/combinations/ 题目描述 ...
- LeetCode 77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 77. Combinations(组合)
题目链接:https://leetcode.com/problems/combinations/ 思路: 回溯,直接用回溯递归,注意边界条件. AC 1ms 100% Java: class Solu ...
- LeetCode 刷题记录 77. Combinations
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 - n. Example: ...
- [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 解题报告(Python C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 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. Exampl ...
最新文章
- websphere、weblogic上JConsole的配置
- 输入一行电报文字,将字母变成其下一字母(如’a’变成’b’……’z’变成’a’其它字符不变)
- pyhton中的wsgi是什么?
- 安装.Net Framework提示:无法建立到信任根颁发机构的证书链
- [Swift]LeetCode528. 按权重随机选择 | Random Pick with Weight
- Springboot使用@Scheduled多节点启动时调度重复执行
- java三猴分桃多线程,浅谈数学趣题:三翁垂钓和五猴分桃
- [NOIP 2018]龙虎斗 题解(Python)
- win10 命令行查看、创建、删除用户
- pytorch Load部分weights
- 英特尔实感3D摄像头
- 进销存仓库管理系统:规范数据、流程与管理
- Matplotlib 箱线图
- Linux笔记1(安装,目录结构,远程登录,vi和vim,用户管理,实用指令。定时调度,挂载。)
- 包头新松机器人_煤矿机器人现状及发展方向
- Python实现的一个简洁轻快的后台管理框架.支持拥有多用户组的RBAC管理后台,不用配置各种运行环境
- Rust 社区 RFC 导读 | 构建安全的 I/O
- 市场营销学4——市场调研与预测
- 射频芯片工作的心脏——晶振
- linux查看用户家目录下的隐藏文件,linux中查看目录下隐藏文件方式?
热门文章
- 江西省中小学生学籍管理-登录(1)
- django之admin调整页面展示
- 数据大屏适配解决方案
- 打地鼠游戏(Appinventor练习)
- android 市场 上传,安卓市场APP上传流程及审核要求
- WORD2013使用技巧——调整序号中制表位的大小
- Why it occurs this error [The JSON value could not be converted to System.Nullable]
- 【EXLIBRIS】#小词旮旯# 006 Wake
- pt-online-schema-change 脚本化
- 怎样用360改计算机名称,360随身wifi网络名称怎么修改