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个数的组合。
这个题我们给出2种解法
第一种:递归
对于任意的n和k,
1.我们先求出从1到n-1中任意k个数的组合
2.然后求,1到n-1中任意k-1个数的组合,每个组合加n,
最后将组合合并到一个集合即可
具体代码:
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种解法在时间空间上存在差异。举个例子:

 我们要求n=9,k=9的组合,显然只有一种组合满足题意,但是当我们使用回溯算法时,tempList分别加入1,2,3,4,5,6,7,8,9之后就不需要再计算了,但事实上,我们仍旧还计算了加入tempList中的第一个元素为2,3,4,5,6,7,8,9的情况,而这些情况显然已经不符合题意了,我觉得这个过程做的无用功是导致这种解法不优的原因。
既然如此,我们在原回溯解法上稍作优化,在回溯方法的第一行加上一个判断条件
if(tempList.size()+n-start+1 < k)return;

假设之后所有元素都加入tempList中都比k小的时候,我们不需要再计算了。

这样一来,运行结果在LeetCode上超越了85%左右,与递归解法相差小了很多;
由此可见,有时候一行代码足以改变一种算法的优劣程度

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 Python3]77. Combinations回溯

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

  3. Leetcode 77. Combinations 组合

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

  4. LeetCode 77. Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. 77. Combinations(组合)

    题目链接:https://leetcode.com/problems/combinations/ 思路: 回溯,直接用回溯递归,注意边界条件. AC 1ms 100% Java: class Solu ...

  6. LeetCode 刷题记录 77. Combinations

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

  7. [leetcode] 77. Combinations @ python

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

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

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

  9. LeetCode之77. Combinations

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

  10. LeetCode | 77. Combinations

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

最新文章

  1. websphere、weblogic上JConsole的配置
  2. 输入一行电报文字,将字母变成其下一字母(如’a’变成’b’……’z’变成’a’其它字符不变)
  3. pyhton中的wsgi是什么?
  4. 安装.Net Framework提示:无法建立到信任根颁发机构的证书链
  5. [Swift]LeetCode528. 按权重随机选择 | Random Pick with Weight
  6. Springboot使用@Scheduled多节点启动时调度重复执行
  7. java三猴分桃多线程,浅谈数学趣题:三翁垂钓和五猴分桃
  8. [NOIP 2018]龙虎斗 题解(Python)
  9. win10 命令行查看、创建、删除用户
  10. pytorch Load部分weights
  11. 英特尔实感3D摄像头
  12. 进销存仓库管理系统:规范数据、流程与管理
  13. Matplotlib 箱线图
  14. Linux笔记1(安装,目录结构,远程登录,vi和vim,用户管理,实用指令。定时调度,挂载。)
  15. 包头新松机器人_煤矿机器人现状及发展方向
  16. Python实现的一个简洁轻快的后台管理框架.支持拥有多用户组的RBAC管理后台,不用配置各种运行环境
  17. Rust 社区 RFC 导读 | 构建安全的 I/O
  18. 市场营销学4——市场调研与预测
  19. 射频芯片工作的心脏——晶振
  20. linux查看用户家目录下的隐藏文件,linux中查看目录下隐藏文件方式?

热门文章

  1. 江西省中小学生学籍管理-登录(1)
  2. django之admin调整页面展示
  3. 数据大屏适配解决方案
  4. 打地鼠游戏(Appinventor练习)
  5. android 市场 上传,安卓市场APP上传流程及审核要求
  6. WORD2013使用技巧——调整序号中制表位的大小
  7. Why it occurs this error [The JSON value could not be converted to System.Nullable]
  8. 【EXLIBRIS】#小词旮旯# 006 Wake
  9. pt-online-schema-change 脚本化
  10. 怎样用360改计算机名称,360随身wifi网络名称怎么修改