leetcode经典组合类问题,采用dfs思想
来源:互联网 发布:自适应网址导航源码 编辑:程序博客网 时间:2024/06/03 21:59
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> combSet=new ArrayList<List<Integer>>();
List<Integer> comb=new ArrayList<Integer>();
if(n<k){
return combSet;
}
helper(n, k, combSet, comb, 1);
return combSet;
}
public void helper(int n, int k, List<List<Integer>> combSet, List<Integer> comb, int start){
if(comb.size()==k){
combSet.add(new ArrayList<Integer>(comb));
return;
}
else{
for(int i=start;i<=n;i++){
comb.add(i);
helper(n, k, combSet, comb, i+1);
comb.remove(comb.size()-1);
}
}
}
}
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> combSet=new ArrayList<List<Integer>>();
List<Integer> comb=new ArrayList<Integer>();
if(n<k){
return combSet;
}
helper(n, k, combSet, comb, 1);
return combSet;
}
public void helper(int n, int k, List<List<Integer>> combSet, List<Integer> comb, int start){
if(comb.size()==k){
combSet.add(new ArrayList<Integer>(comb));
return;
}
else{
for(int i=start;i<=n;i++){
comb.add(i);
helper(n, k, combSet, comb, i+1);
comb.remove(comb.size()-1);
}
}
}
}
阅读全文
0 0
- leetcode经典组合类问题,采用dfs思想
- DFS经典组合问题
- LeetCode Letter Combinations of a Phone Number 经典组合问题
- 经典迷宫问题DFS
- 用进制思想解决组合问题
- DFS求组合的问题~
- Leetcode全组合问题
- 递归思想解决经典问题
- 棋盘问题(经典DFS)
- N皇后问题<经典DFS>
- 组合数学经典问题---铺砖问题
- LeetCode—*组合问题Combinations
- 采用归并排序思想解决逆序对数问题
- LeetCode 47. Permutations II(生成不同的组合-dfs)
- POJ 1011 DFS的经典问题
- hdu 2553 N皇后问题 (经典DFS)
- 经典迷宫问题DFS 深度优先
- 经典DFS之N皇后问题
- Caused by: org.hibernate.PropertyNotFoundException:
- 实现一个栈Stack,要求实现Push(入栈)、Pop(出栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- hdu3357Stock Chase (暴搜)
- 同济大学Openwrt路由器 ipv6教程(新手导向)
- httpclient连接池在ES Restful API请求中的应用
- leetcode经典组合类问题,采用dfs思想
- 【转】多个类定义attr属性重复的问题:Attribute "xxx" has already been defined
- VMWARE 简单的操作
- Ubuntu16.04下安装OpenCV-3.2.0
- python求解LeetCode题目,找出数组中的Majority element元素
- 快速排序总结
- 流形上的切空间
- HDU 1565 方格取数(1)(插头DP||状态压缩)
- web day015sql