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 ...
随机推荐
- URI、URL以及URN的区别
首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ...
- xv6实验环境搭建
安装bochs 因为要运行的是xv6,所以不能直接使用 apt-get 直接获取软件.apt-get获取到的软件不支持SMP (Symmetric Multi-Processing).因此,需要下载源 ...
- 菜鸟学习Struts——配置Struts环境
刚开始学习Struts,它通过采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架,是MVC经典设计模式中的一个经典产品. 要用到Struts就要学会配 ...
- Access
一般系统的实现: 管理系统的分析与设计 --->>数据表的设计创建 --->> 设计“查询”与“宏” --->> 创建窗体与报表 --->>系统注册 启 ...
- C++中用辗转相除法求两个数的最大公约数和最小公倍数
两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0? max=min,min=res return min; ...
- Team Homework #3: The feedback of predecessors
此次对学长的采访主要在QQ上进行,感谢陈宇宁学长的热情配合. 采访学长的问题及学长的答复如下: 1. 平均每周花在这门课上的时间 (包括上课/作业/上机) -大约15-20小时吧(学长个人花费时间) ...
- 怎么直接在MySQL客户端上执行SQl文件?
\. 直接把sql文件拖进去就行了,(斜杠+.+空格+sql文件)
- iOS10和Xcode8适配
1 Xib文件的注意事项 使用Xcode8打开xib文件后,会出现下图的提示. 大家选择Choose Device即可. 之后大家会发现布局啊,frame乱了,只需要更新一下frame即可.如下图 注 ...
- c语言编程之栈(数组实现)
用数组实现的顺序栈,完成了出栈入栈功能. #include"stdio.h" typedef int element; #define max 100 typedef struct ...
- 1067: [SCOI2007]降雨量 - BZOJ
Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2 ...