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],
]

原题链接:https://oj.leetcode.com/problems/combinations/

题目:给定两个整数n和k,返回全部的k个1……n中的数的组合。

思路:递归。

	public List<List<Integer>> combine(int n, int k) {
return combine(1, n + 1, k);
} public List<List<Integer>> combine(int low, int upper, int k) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (k == 1) {
for (int i = low; i < upper; i++) {
List<Integer> r = new ArrayList<Integer>();
r.add(i);
result.add(r);
}
return result;
}
for (int i = low; i < upper; i++) {
List<List<Integer>> r = combine(i + 1, upper, k - 1);
for (List<Integer> a : r) {
a.add(0, i);
}
result.addAll(r);
}
return result;
}

reference : http://www.laurashawn.net/?p=10907

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

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

  3. [leetcode]Combinations @ Python

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

  4. [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 ...

  5. [LeetCode] Combinations [38]

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

  6. LeetCode: Combinations 解题报告

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

  7. [Leetcode] combinations 组合

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

  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 ...

随机推荐

  1. 理解ROS的节点(NODE)

    经过前面的学习,我们已经知道了如何构建一个ROS的包,这篇博客将介绍ROS中的节点的概念. 在继续之前,请按ctrl+alt+t打开一个终端,在里面输入: sudo apt-get install r ...

  2. Apache 编译扩展的方法

    下载源码包 进入源码包的modules目录 选择你要编译的.c文件 eg: /home/work/local/apache/bin/apxs -c -i -a mod_proxy_http.c 选项说 ...

  3. Android ActionBar详解(二)--->使用ActionBar显示选项菜单

    MainActivity如下: package cc.testsimpleactionbar1; import android.os.Bundle; import android.app.Activi ...

  4. Main方法中传入参数

    ↓ 这个时候会报错: Instantiate the class:DonutShop java.lang.ClassNotFoundException: DonutShop    at java.ne ...

  5. APM代码学习笔记2:编译过程

    make编译 所有位置的Makefile 引用的都是/mk/apm.mk target.mk 设置CONFIG_HAL_BOARD 例如linux就是HAL_BOARD_LINUX environ.m ...

  6. MYSQL alter procedure alter function 它们只可以更改过程的特性,不可以更改过程的逻辑。

    例子: delimiter // create procedure proc_a(in numberA int) 这样create procedure 是正确的 begin select number ...

  7. Windows 技巧

    1.反选 快捷键: Alt+E+I 2. windows7 以上 cmd命令 切换目录 F:\>cd /d c:\windowsc:\Windows> 3.

  8. CSS 常用自定义样式

    目录: 1. 文本单行显示,并对超出部分截断以省略号代替: 2.列布局或栅格布局:比如:左侧固定宽度,右侧占满剩下的宽度: 章节: 1. 文本单行显示,并对超出部分截断以省略号代替:参见以下代码: d ...

  9. .Net时间计算函数,统计某一天是一年的第几周,这一周从哪天开始到哪天结束

    /// <summary> /// 计算某年第一周的天数         /// </summary>         /// <param name="dt& ...

  10. Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法

    安装apache2 sudo apt-get install apache2 安装完成,运行如下命令重启下: sudo /etc/init.d/apache2 restart 在浏览器里输入http: ...