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

For example,
If n = 4 and k = 2, a solution is: [
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> list=new ArrayList<List<Integer>>();
List<Integer> item=new ArrayList<Integer>();
backTracking(n, k, 1, item, list);
return list;
} public void backTracking(int n, int k, int start, List<Integer> item, List<List<Integer>> list){
if(item.size()==k){
list.add(new ArrayList<Integer>(item));
return;
}
for(int i=start; i<=n; i++){
item.add(i);
backTracking(n, k, i+1, item, list);
item.remove(item.size()-1);
} }
}

LeetCode-Combinations的更多相关文章

  1. [LeetCode] Combinations 组合项

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

  2. LeetCode——Combinations

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

  3. leetcode — combinations

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  4. [leetcode]Combinations @ Python

    原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...

  5. [LeetCode] Combinations (bfs bad、dfs 递归 accept)

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

  6. [LeetCode] Combinations [38]

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

  7. LeetCode: Combinations 解题报告

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

  8. [Leetcode] combinations 组合

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

  9. [LeetCode] Combinations 回溯

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

  10. [LeetCode] Combinations——递归

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

随机推荐

  1. 超简单的js数字验证

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  2. When you install printer in Ubuntu, just need a ppd file.

    Search printing in the system and add printer. Then import ppd file. That is all.

  3. 利用border-radious画图形

    今天才发现,border-radius可以画很多图形,下面跟我来看一下吧: 在设有宽和高的情况下画一个圆: #div1{ /*宽高相等,圆角范围为高或宽的一半或以上*/ background-colo ...

  4. 【C】制作简单的时钟减法程序

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define ARGC_NUM 3 #def ...

  5. Excel 改变列表头显示方式, Excel显示列数字

    '显示数字列号 Sub showCellNumber() Application.ReferenceStyle = xlR1C1 End Sub '显示字母列号 Sub showCellZimu() ...

  6. C++复数类对除法运算符 / 的重载

    C8-1 复数加减乘除 (100.0/100.0 points) 题目描述 求两个复数的加减乘除. 输入描述 第一行两个double类型数,表示第一个复数的实部虚部 第二行两个double类型数,表示 ...

  7. 【搬砖】【Python数据分析】Pycharm中plot绘图不能显示出来

    最近在看<Python数据分析>这本书,而自己写代码一直用的是Pycharm,在练习的时候就碰到了plot()绘图不能显示出来的问题.网上翻了一下找到知乎上一篇回答,试了一下好像不行,而且 ...

  8. Cruehead.1

    查壳   没有 我拖 alt+F9 到上面        入口处   下断 关键跳      略过   就没了 要实现 强暴  直接过... 仔细来看看... 那两个调用   都下断   看看  判断 ...

  9. amazon oa2 - insert a value into a cycled linked list

    遍历,一共有三种情况, 1. pre <= x <= current 2. 遍历到了head时,x>tail 或者 x<=head (不会等于tail) 3. 遍历回aNode ...

  10. DotNetBar 第2课,窗口设置 Ribbon Form 样式

    1. 新增 windows 窗体时,选 Ribbon Form 2. 窗体继承 Office2007RibbonForm 3. 设计窗口下面,删除 删除styleManager1  组件 窗口效果如下 ...