【1】【leetcode-77】 组合
(典型,做过似曾相识但不熟悉,基本知道怎么做但调试了一个多小时各种错)
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
] 我的:
public class Solution77 {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
//为了不重复,list中只能是顺序
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
if (k > n || k<0 || n<0) {
return res;
}
ArrayList<Integer> list = new ArrayList<>();
helper(n,k,1,list);
return res;
}
// 还能加入k个数
public void helper(int n, int k,int start,ArrayList<Integer> list) {
if (k == 0) {
res.add(new ArrayList<>(list)); // 易错点1
return;
}
for (int i=start;i<=n-k+1;i++) {
list.add(i);
helper(n,k-1,i+1,list);
list.remove(list.size()-1); // 易错点2
}
}
}
参考:
链接:https://www.nowcoder.com/questionTerminal/4d0a110416d84c7f9454d0da53ab2da1
来源:牛客网 import java.util.ArrayList;
//使用剪枝对算法进行了优化;
//Your runtime beats 99.50 % of java submissions
public class Solution {
private ArrayList<ArrayList<Integer>> res; public ArrayList<ArrayList<Integer>> combine(int n, int k) {
res = new ArrayList<ArrayList<Integer>>();
if (n <= 0 || k <= 0 || n < k)
return res;
generateCombinations(n, k, 1, new ArrayList<Integer>()); return res;
} private void generateCombinations(int n, int k, int start, List<Integer> list) {
if (list.size() == k) {
res.add(new ArrayList<Integer>(list));
return;
}
if (start > n)
return; int len = k - (list.size() + 1);
//list当中最终应该有k个元素,当前元素为list.size() + 1,那么我们要为下次回溯留下足够多的数
for (int i = start; i <= n - len; i++) {
list.add(i);
generateCombinations(n, k, i + 1, list);
list.remove(list.size() - 1);
}
} }
【1】【leetcode-77】 组合的更多相关文章
- Java实现 LeetCode 77 组合
77. 组合 给定两个整数 n 和 k,返回 1 - n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- LeetCode 77. 组合(Combinations)
题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- Leetcode之回溯法专题-77. 组合(Combinations)
Leetcode之回溯法专题-77. 组合(Combinations) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- leetcode排列组合相关
目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...
- LeetCode 77,组合挑战,你能想出不用递归的解法吗?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- GGS-DDU HDU - 4966
GGS-DDU Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- windows 虚拟环境下 安装 mysql 引擎一系列错误处理
报错现象 运行django 报错. 很明显是缺少引擎 下载引擎 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb m ...
- 【CodeForces 730H】Delete Them
BUPT 2017 summer training (for 16) #1E 题意 找到匹配要删除的文件名们但不匹配其它文件名们的表达式.其中?匹配所有字符,其它字符匹配本身. 题解 如果某个位置出现 ...
- python学习日记(迭代器、生成器)-乱七八糟
迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退 ...
- 【刷题】LOJ 556 「Antileaf's Round」咱们去烧菜吧
题目描述 你有 \(m\) 种物品,第 \(i\) 种物品的大小为 \(a_i\) ,数量为 \(b_i\)( \(b_i=0\) 表示有无限个). 你还有 \(n\) 个背包,体积分别为 \(1 ...
- Hdoj 1115.Lifting the Stone 题解
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- Hdoj 2717.Catch That Cow 题解
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- noiac26 T1 (并查集)
考虑计算每个位置的数作为最小值时有多少种情况 方便起见,以位置为第二关键字比较大小,这样就不会出现“相同的”数 可以方便地计算出以i位置为最小值的区间端点的可行位置:[A,i],[i,B] 这是我选的 ...
- poj2373 Dividing the Path (单调队列+dp)
题意:给一个长度为L的线段,把它分成一些份,其中每份的长度∈[2A,2B]且为偶数,而且不能在某一些区间内部切开,求最小要分成几份 设f[i]为在i处切一刀,前面的满足要求的最小份数,则f[L]为答案 ...
- hdu2586How far away ?(LCA LCATarjan离线)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:有n个点,同n-1条带有权值的双向边相连,有m个询问,每个询问包含两个数x,y,求x与y的最短距离. ...