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. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  2. R与数据分析旧笔记(一)基本数学函数的使用

    创建向量矩阵 > x1=c(2,3,6,8) > x2=c(1,2,3,4) > a1=(1:100) > length(a1) [1] 100 > length(x1) ...

  3. codeforces 633G. Yash And Trees dfs序+线段树+bitset

    题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...

  4. [LeetCode]题解(python):136-Single Number

    题目来源: https://leetcode.com/problems/single-number/ 题意分析: 给定一个数组,每个数都出现了2次,只有一个出现了一次,找出这个数.要求时间复杂度O(n ...

  5. delphi datasnap 心跳包

    为了能让我们的服务程序更加稳定,有些细节问题必须解决.就如上一讲中提到的客户端拔掉网线,造成服务器上TCP变成死连接,如果死连接数量过多,对服务器能长期稳定运行是一个巨大的威胁.另外,经过测试,如果服 ...

  6. DLL编写教程(绝对经典之作)

    DLL编写教程 半年不能上网,最近网络终于通了,终于可以更新博客了,写点什么呢?决定最近写一个编程技术系列,其内容是一些通用的编程技术.例如DLL,COM,Socket,多线程等等.这些技术的特点就是 ...

  7. HTML语法

    2014-01-21/21:28:59 网页制作概述 HTML是WEB页面的描述性语言 CSS为HTML样式机制 JavaScript为交互WEB设计最佳选择 CSS\JavaScript\PHP都可 ...

  8. jar包问题

    解决方法: 1右键项目build path的lib的add jars添加进来

  9. 线性规划问题的matlab求解

    函数:[x, fval] = linprog(f, A, b, Aeq, Beq, LB, UB) 返回的x:是一个向量——在取得目标函数最小时各个xi的取值: 返回的fval:目标函数的最小值: 参 ...

  10. window.showModalDialog刷新父窗口和本窗口的方法及注意

    window.showModalDialog刷新父窗口和本窗口的方法及注意:   一.刷新父窗口的方法:    A.使用window.returnValue给父窗口传值,然后根据值判断是否刷新. 在w ...