题目:

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],
]
Hide Tags

Backtracking 

链接:  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的更多相关文章

  1. Leetcode 77, Combinations

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

  2. 77. Combinations(medium, backtrack, 重要, 弄了1小时)

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

  3. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  4. [leetcode]77. Combinations组合

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

  5. 77. Combinations (java 求C(n,k)的组合,排除重复元素)

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...

  6. (效率低下)77. Combinations C++回溯法 组合

    https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...

  7. LeetCode OJ 77. Combinations

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

  8. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  9. 77. Combinations(回溯)

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

随机推荐

  1. C程序调用shell脚本共有三种方法

    C程序调用shell脚本共有三种法子 :system().popen().exec系列函数call_exec1.c ,内容为:system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令e ...

  2. python自学笔记二

    :#进入循环重输文0件名 pass else:#退出循环,等待创建 break fobj = open(fname,'a')#打开或创建文件 #接下来写入文件 all = [] print('ente ...

  3. Oracle查询出最最近一次的一条记录

    需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...

  4. Java集合的小抄

    在尽可能短的篇幅里,将所有集合与并发集合的特征.实现方式.性能捋一遍.适合所有"精通Java",其实还不那么自信的人阅读. [转自:花钱的年华] 期望能不止用于面试时,平时选择数据 ...

  5. IE中出现 "Stack overflow at line" 错误的解决方法

    在做网站时遇到一个问题,网站用的以前的程序,在没有改过什么程序的情况下,页面总是提示Stack overflow at line 0的错误,而以前的网站都正常没有出现过这种情况,在网上找了一下解决办法 ...

  6. mapreduce 实现矩阵乘法

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...

  7. zip压缩包密码破解

    有一种破解方法叫做Known plaintext attack.市面上的密码破解软件几乎都带有这个功能.操作方法就是找到加密压缩包中的任意一个文件,用同样的压缩软件同样的压缩方式压缩成一个不加密的包, ...

  8. 人工智能起步-反向回馈神经网路算法(BP算法)

    人工智能分为强人工,弱人工. 弱人工智能就包括我们常用的语音识别,图像识别等,或者为了某一个固定目标实现的人工算法,如:下围棋,游戏的AI,聊天机器人,阿尔法狗等. 强人工智能目前只是一个幻想,就是自 ...

  9. JAVA类与对象(一)----基础概念理解

    面向对象基本概念 面向对象是一种新兴的程序设计方法,或者说是一种新的程序设计规范,其基本思想是使用对象.类.继承.封装.消息等基本概念来进行程序设计.它是从现实世界客观存在的事物(即对象)出发来构造软 ...

  10. 常用的机器学习&数据挖掘知识点【转】

    转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...