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

Example:

Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
 class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
List<Integer> temp = new ArrayList<Integer>();
help(temp,n,k,1);
return res; }
private void help(List<Integer> temp,int n ,int k ,int index){
if(k==0){
res.add(new ArrayList<Integer>(temp));
}
for(int i = index;i<=n;i++){
temp.add(i);
help(temp,n,k-1,i+1);
temp.remove(temp.size()-1);
}
}
}

77. Combinations(回溯)的更多相关文章

  1. (效率低下)77. Combinations C++回溯法 组合

    https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...

  2. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. 77. Combinations (Recursion)

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

  4. [LeetCode] Combinations 回溯

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

  5. 77. Combinations (JAVA)

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

  6. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  7. Leetcode 77, Combinations

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

  8. 77. Combinations

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  9. 77. Combinations(medium, backtrack, 重要, 弄了1小时)

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

随机推荐

  1. swift开发之 -- 自动轮播图(UIScrollView+UIPageControl+Timer)

    比较简单,原理就不说了,这里只做记录: 代码如下: 1,准备 var pageControl:UIPageControl? var myscrollView:UIScrollView? var myT ...

  2. python远程登录服务器(paramiko模块安装和使用)

    转自:http://www.jb51.net/article/46285.htm 一:简介 由paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器 ...

  3. divmod()

    divmod() 接收两个数值,然后以元组的形式返回这两个数值的商和余数 In [1]: divmod(5, 2) Out[1]: (2, 1) In [2]: divmod(10, 7) Out[2 ...

  4. 《C++ Primer Plus》14.3 多重继承 学习笔记

    多重继承(MI)描述的是有多个直接基类的类.与单继承一样,共有MI表示的也是is-a关系.例如,可以从Awiter类和Singer类派生出SingingWaiter类:class SingingWai ...

  5. Android 7.1.1 系统截屏

    frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java Tak ...

  6. python的三个函数(eval、exec、complie)和python版RMI

    一.python的三个函数: 1.eval函数: 之前已经讲过了这个函数,该函数也类似于php的eval,例如下边这个例子 eval("os.system('id')") 但是有个 ...

  7. Cookie的介绍及使用

    咱们不搞一开始就一大堆理论知识介绍,怕把人讲懵了...... 咱们换一个思维方式——"从现象看本质",先说说我们看到了什么,再从看到的现象中提出问题,最后深入寻找答案. 我们看到的 ...

  8. android 点九工具介绍

    1=============================9.png简介:“点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png android平台有多种不同的 ...

  9. 最舒适的路(并查集+枚举)(hdu1598)

    hdu1598 find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768 ...

  10. html 标准属性不要用 setAttribute 方法

    html 中有些属性,譬如 checked , autofocus 只要存在就会有效,即使值是 false ,譬如: <input autofocus=false id='test'> & ...