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

给定两个整数 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. 实验六 MapReduce实验:二次排序

    实验指导: 6.1 实验目的基于MapReduce思想,编写SecondarySort程序. 6.2 实验要求要能理解MapReduce编程思想,会编写MapReduce版本二次排序程序,然后将其执行 ...

  2. 【CF1009F】Dominant Indices(长链剖分)

    [CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...

  3. python列表转字符串

    temp = "".join(sorted(arr[i])) arr[i] = temp

  4. poj1845 sumdiv (因数的和)

    首先分解质因数,$A^B=p_1^{m_1B}p_2^{m_2B}...p_n^{m_nB}$ 然后的话,它的所有因数的和就是$\prod{(1+p_i^1+p_i^2+...+p_i^n)}$ 用一 ...

  5. MySQL -- 单行函数

    大小写控制函数 SELECT LOWER('HelloWrold'), UPPER('HelloWorld'); 字符控制函数 SELECT REPLACE('abcdababab','p','m') ...

  6. Weblate 2.11安装配置文档

    一.系统环境: OS:CentOS 6.8 x64 Minimal HostName:Weblate IP:192.168.75.153 Python:2.7.13 pip:9.0.1 Weblate ...

  7. 关于数据结构,剑指offer上面的

    我很喜欢那些javascript解决的编程题,感觉非常的有意思.我在博客园上面看到了一个同学的博客,他一共发了34篇剑指offer的编程题,还给出了非常详细的解答. 接下来的工作,我做的就是搬运工,不 ...

  8. 查看已安装tensorflow版本

    http://blog.csdn.net/u011961856/article/details/76861052 由于tensorflow版本不同,可能一些函数的调用也有变换,这时候可能需要查看ten ...

  9. eclipse安装activiti 工作流插件

    记录一下下eclipse集成activiti插件的过程. eclipse的版本信息为:Version: Mars.1 Release 1 (4.5.1) 下面就开始介绍下如何安装activiti插件. ...

  10. Day037--Python--线程的其他方法,GIL, 线程事件,队列,线程池,协程

    1. 线程的一些其他方法 threading.current_thread()  # 线程对象 threading.current_thread().getName()  # 线程名称 threadi ...