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. day3_python学习笔记_chapter5_数字

    1. 整形的表示范围-2^32~2^32 - 1 : 长整形表示:aLong = 99999L 2. 复数的属性, num.real,该复数的实部, num.imag,该复数的虚部.num.conju ...

  2. ThinPHP第二十八天(F函数和file_put_contents区别|PHP生成PHP文件,Kindeditor编辑器使用方法)

    1.F(name,data,path)函数和file_put_contents(file,str)区别 F函数直接生成<?php ?>格式的php文件了,将data加入到<?php和 ...

  3. LaTeX空格

    由于LaTeX 采用的是源文件编译方式,  默认LaTeX会忽略多余的空格, 如果需要产生一个空格,可以使用 命令 \, 注意代表的是空间键. 例如: Jones, et al.\  (1993), ...

  4. SQL Server 数据类型陷阱

    1. bit 类型:bit(1) 不要以为它只占一个位,事实上它要占一个字节!也就是说当n < 8 时都是这样的! 2. varchar(n)  这里的n不能大于8000,如果想要比8000大你 ...

  5. QCombobox设置下拉框的宽度

    这几天写一个项目,里面用到qcombobox组件,其中下拉框含有129个子项,所以在点击的时候,一个下拉框就将整个电脑屏幕给占满了,很不好看并且在使用中会造成很大的苦恼.其实我就是想设置一个下拉框最大 ...

  6. bean 与 map 互转.

    package com.sprucetec.tms.distribute.utils;import java.beans.BeanInfo;import java.beans.Introspectio ...

  7. 使用自定义脚本扩展程序自动执行 VM 自定义任务

     在 Build 开发者大会上推出VM 扩展程序的其中一个称为"自定义脚本扩展程序",它支持 PowerShell.如果这是您第一次访问这些博客,可能需要查看以前的博客,请单击 ...

  8. start stack

    Start OpenStack Services After launching your stack by Devstack, you maybe stop some services or reb ...

  9. Nhibernate初入门基本配置(二)

    转载地址http://www.cnblogs.com/kissdodog/p/3306428.html 使用NHibernate最重要的一步就是配置,如果连NHibernate都还没有跑的起来,谈何学 ...

  10. Lucene站内搜索的设计思路

    正好近期部门有一个小需求需要做商品的搜索,虽然最终由于工作量等原因先做数据库搜索,我依然用刚接触的Lucene弄了一套自嗨. 首先看需求:搜索:根据商品标题和内容搜索 没错,就这么简单! 我想了想,数 ...