(典型,做过似曾相识但不熟悉,基本知道怎么做但调试了一个多小时各种错)

给定两个整数 n 和 k,返回 1 ... 中所有可能的 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】 组合的更多相关文章

  1. Java实现 LeetCode 77 组合

    77. 组合 给定两个整数 n 和 k,返回 1 - n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

  2. LeetCode 77. 组合(Combinations)

    题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

  3. Leetcode之回溯法专题-77. 组合(Combinations)

    Leetcode之回溯法专题-77. 组合(Combinations)   给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...

  4. LeetCode:组合总数III【216】

    LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...

  5. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  6. leetcode排列组合相关

    目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...

  7. LeetCode 77,组合挑战,你能想出不用递归的解法吗?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...

  8. LeetCode 77 Combinations(排列组合)

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

  9. [LeetCode] 77. Combinations 全组合

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

  10. leetCode 77.Combinations (组合)

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

随机推荐

  1. 安装PHP 类库PEAR

    PHP扩展与应用库常识 当php项目里没有pear时: 单独安装解决方案 下载下面连接的文件至go-pear.phar.http://pear.php.net/go-pear.phar该文件最好放到P ...

  2. 【Tsinsen A1039】【bzoj2638】黑白染色 (BFS树)

    Descroption 原题链接 你有一个\(n*m\)的矩形,一开始所有格子都是白色,然后给出一个目标状态的矩形,有的地方是白色,有的地方是黑色,你每次可以选择一个连通块(四连通块,且不要求颜色一样 ...

  3. 【BZOJ3925】[ZJOI2015]地震后的幻想乡(动态规划)

    [BZOJ3925][ZJOI2015]地震后的幻想乡(动态规划) 题面 BZOJ 洛谷 题解 题目里面有一句提示:对于\(n\)个\([0,1]\)之间的随机变量\(x1,x2,...,xn\),第 ...

  4. Ubuntu相关配置

    1.Ubuntu配置root密码 2.开机ssh连接--nat   编辑网络设置端口转发

  5. poj2524(并查集水题)

    题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...

  6. redux源码解析-函数式编程

    提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript> ...

  7. spring activemq 整合

    创建maven项目 项目目录结构为 首先配置相关maven依赖 <!-- 版本管理 --> <properties> <springframework>4.1.8. ...

  8. yzh的神仙题

    U66905 zz题 考虑一个点权值被计算了多少次...不知 所以对未来承诺,方便直接算上总数! 然后其实是给边定向,即先删除fa和son的哪一个 f[x][j],会计算j次 无法转移 f[x][j] ...

  9. 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,nloglogutil

    封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创 ...

  10. Cannot read property 'properties' of undefined

    今天运行一个很有意思的项目,报上面的错 根据报错,可以发现,报错出现在项目文件夹下的node_modules\webpack-cli\bin\config-yargs.js文件第89行. 当前webp ...