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],
]
链接: http://leetcode.com/problems/combinations/
题解:
求组合数。依然是使用recursive + backtracking,当所传list的大小等于k时,将list加入到result里。使用控制位置的变量pos根据题目要求从1开始,本体依然假定无重复数字。
Time Complexity - O(2n), Space Complexity - O(n)。 --复杂度有点糊涂,还需要重新计算
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(n < 0 || k < 0)
return result;
ArrayList<Integer> list = new ArrayList<Integer>();
helper(result, list, n, k, 1);
return result;
}
private void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int n, int k, int pos){
if(list.size() == k){
result.add(new ArrayList<Integer>(list));
return;
}
for(int i = pos; i <= n; i++){
list.add(pos);
helper(result, list, n, k, ++pos);
list.remove(list.size() - 1);
}
}
}
Update:
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if(n < 0 || k < 0)
return res;
ArrayList<Integer> list = new ArrayList<Integer>();
dfs(res, list, n, k, 1);
return res;
}
private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int n, int k, int position) { // from 1 to n
if(list.size() == k) {
res.add(new ArrayList<Integer>(list));
return;
}
for(int i = position; i <= n; i++) {
list.add(i);
dfs(res, list, n, k, i + 1);
list.remove(list.size() - 1);
}
}
}
二刷
有关这一类排列组合题目有不少题型,一定要好好思考总结融会贯通。
依然是跟1刷类似的方法,使用DFS + Backtracking。要注意的是最后的结果是(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)这类一定是升序的组合,所以我们构建辅助函数的时候只要pass in 一个position来控制就可以了。递归的时候传入 combine(res, list, k, n, i + 1)这样就能简单避免掉i,只丛i后面的数字继续遍历。
这里branching factor是n, depth是k。 所以其实时间复杂度是 O(n! / k!) 约等于 O(n!)
Time Complexity - O(n!), Space Complexity - O(nk)
Java:
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
combine(res, list, n, k, 1);
return res;
}
private void combine(List<List<Integer>> res, List<Integer> list, int n, int k, int pos) {
if (list.size() == k) {
res.add(new ArrayList<Integer>(list));
return;
}
for (int i = pos; i <= n; i++) {
list.add(i);
combine(res, list, n, k, i + 1);
list.remove(list.size() - 1);
}
}
}
三刷:
方法跟二刷一样。也是构造递归求解的辅助方法来解决。注意这里我们也需要一个保存position的int pos。
有机会要再算一算复杂度。
Java:
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if (n < 0 || k < 0 || n < k) return res;
getCombinations(res, new ArrayList<Integer>(), n, k, 1);
return res;
}
private void getCombinations(List<List<Integer>> res, List<Integer> list, int n, int k, int pos) {
if (list.size() == k) {
res.add(new ArrayList<>(list));
return;
}
for (int i = pos; i <= n; i++) {
list.add(i);
getCombinations(res, list, n, k, i + 1);
list.remove(list.size() - 1);
}
}
}
Reference:
http://www.1point3acres.com/bbs/thread-117602-1-1.html
http://codeganker.blogspot.com/2014/03/combinations-leetcode.html
http://www.cnblogs.com/zhuli19901106/p/3485751.html
http://www.1point3acres.com/bbs/thread-117602-1-1.html
https://leetcode.com/discuss/42034/java-solution-easy-understood
https://leetcode.com/discuss/30221/dfs-recursive-java-solution
https://leetcode.com/discuss/61607/ac-python-backtracking-iterative-solution-60-ms
https://leetcode.com/discuss/37021/1-liner-3-liner-4-liner
https://leetcode.com/discuss/24600/iterative-java-solution
https://leetcode.com/discuss/12915/my-shortest-c-solution-using-dfs
https://leetcode.com/discuss/31250/backtracking-solution-java
http://rangerway.com/way/algorithm-permutation-combination-subset/
http://www.1point3acres.com/bbs/thread-117602-1-1.html
http://www.cnblogs.com/zhuli19901106/p/3492515.html
77. 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(medium, backtrack, 重要, 弄了1小时)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【一天一道LeetCode】#77. Combinations
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- 77. Combinations (java 求C(n,k)的组合,排除重复元素)
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...
- (效率低下)77. Combinations C++回溯法 组合
https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- 77. Combinations(回溯)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
随机推荐
- Intel格式和AT&T格式汇编区别
一.AT&T 格式Linux 汇编语法格式 在 AT&T 汇编格式中,寄存器名要加上 '%' 作为前缀:而在 Intel 汇编格式中,寄存器名不需要加前缀.例如: AT&T 格 ...
- python之else总结
python中除了if...elif...else..还有while...else, for...else..., try...except...else...finally... 不管哪种else, ...
- 【rest】 深入理解rest
起因是想搞明白 ajax.rest风格和http请求数据会有什么区别 再来回顾一下概念: REST即表述性 状态 传递 满足这些约束条件和原则的应用程序或设计就是RESTful.需要注意的是,REST ...
- 设置NODE_ENV=production
NodeJS - Express 4.0下设置环境变量NODE_ENV=production,并不是修改文件的配置信息,而是通过命令行来实现. 首先在命令行下进入项目的目录,然后先后执行如下命令: s ...
- VBS基础篇 - FileSystemObject对象
文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs中对桌面和文件系统进行访问的顶级对象是FileSystemObject FSO包含的常见对象有: ...
- VBS基础篇 - 动态数组
VBS中的动态数组需要使用System.Collections.ArrayList '建立动态数组 Dim Arrl : Set Arrl = CreateObject("System.Co ...
- Careercup - Facebook面试题 - 6299074475065344
2014-05-01 01:00 题目链接 原题: Given a matrix with 's. What is the maximum area of the rectangle. In . Ho ...
- 2124: 等差子序列 - BZOJ
Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一个整数T,表示组数.下接 ...
- MAC 13信道
房东的路由器一直连不上,手机却能连上,前天设置了13信道,后来又忘了,最后找到个连接WIFI的方法,在网络偏好设置里选择向导,诊断中可以连上wifi.
- 在Eclipse中使用Propertites Editor插件来解决property文件中文显示乱码
在一般情况下,propertites文件在eclipse中的显示中文一直显示乱码,想要解决这个问题,需要通过在eclipse中安装一个Propertites Editor插件来进行解决. 在Eclip ...