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

给定两个整数 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. navicat激活

    参考:https://www.jianshu.com/p/5f693b4c9468 一开始想激活12.1.8,但是激活按钮一直点不了,换了个12.0激活成功

  2. vsftpd 安装与配置

    下载安装vsftpd服务,db4用来支持文件数据库yum install -y vsftpd db4-utils ftp 建立宿主用户 vsftpduseradd -s /sbin/nologin - ...

  3. Hdoj 1050.Moving Tables 题解

    Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a buildin ...

  4. Leetcode 27.移除元素 By Python

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  5. 【BZOJ3174】[TJOI2013]拯救小矮人(贪心,动态规划)

    [BZOJ3174][TJOI2013]拯救小矮人(贪心,动态规划) 题面 BZOJ 洛谷 题解 我们定义一个小矮人的\(A_i+B_i\)为它的逃跑能力. 我们发现,如果有两个小矮人\(x,y\), ...

  6. 纯CSS画的基本图形(圆形、三角形、多边形、爱心、八卦等)

    1.圆形 .circle { width: 100px; height: 100px; background: red; border-radius: 50px; } 2.椭圆 .oval { wid ...

  7. Haunted Graveyard ZOJ - 3391(SPFA)

    从点(n,1)到点(1,m)的最短路径,可以转换地图成从(1,1)到(n,m)的最短路,因为有负权回路,所以要用spfa来判负环, 注意一下如果负环把终点包围在内的话, 如果用负环的话会输出无穷,但是 ...

  8. CF1106E Lunar New Year and Red Envelopes

    比赛时看到这题懵逼了,比完赛仔细一想是个很简单的dp = = 由于题目限制,可以发现\(B\)取红包的策略是唯一的,可以用优先队列预处理出\(B\)在第\(i\)秒可以拿到的红包的收益\(w_i\)和 ...

  9. MYSQL主从复制制作配置方案

    1. 主从复制机器配置 操作系统:centos7 x64 基于vagrant下的virtual box的虚拟机两台 master ip:192.168.21.11, slave ip 192.168. ...

  10. JavaScript深入之变量对象

    前言 在上篇<javascript深入之执行上下文栈>中讲到,当javascript代码执行一段可执行代码(executable code)时,会创建对应的执行上下文(execution ...