import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/combinations/
*
*
* 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 Combination {
private List<Integer[]> result = null; /**
* 求排列组合Cnk
* 使用递归求解
* 或者使用针对每一位寻找剩余位数
* @param n
* @param k
* @return
*/
public List<Integer[]> combine (int n, int k) {
result = new ArrayList<Integer[]>();
List<Integer> list = new ArrayList<Integer>();
combineByRecursion(n, 1, k, list);
return result;
}
public List<Integer[]> combine1 (int n, int k) {
result = new ArrayList<Integer[]>();
List<Integer> list = new ArrayList<Integer>();
combineByRecursion1(n, k, list);
return result;
} /**
* 从前向后递归
* @param n
* @param start
* @param k
* @param list
*/
public void combineByRecursion (int n, int start, int k, List<Integer> list) {
if (k == 0) {
Integer[] newList = new Integer[list.size()];
System.arraycopy(list.toArray(new Integer[list.size()]), 0, newList, 0, list.size());
result.add(newList);
return;
}
for (int i = start; i <= n; i++) {
list.add(i);
combineByRecursion(n, i + 1, k - 1, list);
list.remove(list.size() - 1);
}
} /**
* 从后向前递归
* @param n
* @param k
* @param list
*/
public void combineByRecursion1 (int n, int k, List<Integer> list) {
if (k == 0) {
Integer[] newList = new Integer[list.size()];
System.arraycopy(list.toArray(new Integer[list.size()]), 0, newList, 0, list.size());
result.add(newList);
return;
}
for (int i = n; i > 0; i--) {
list.add(i);
combineByRecursion1(i - 1, k - 1, list);
list.remove(list.size() - 1);
}
} /**
* 不使用递归,使用循环
* @param n
* @param k
*/
public void combine2 (int n, int k) {
for (int i = 1; i <= n-k; i++) {
for (int j = i + 1; j < n-k; j++) { } }
} public static void print (List<Integer[]> list) {
for (Integer[] arr : list) {
System.out.println(Arrays.toString(arr));
}
System.out.println();
} public static void main(String[] args) {
Combination combination = new Combination();
print(combination.combine(4, 2));
print(combination.combine1(4, 2));
}
}

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 @ 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. 2019.03.26 bzoj4448: [Scoi2015]情报传递(归并排序+树链剖分)

    传送门 题意简述: 给一棵nnn个点的树,树上每个点表示一个情报员,一共有mmm天,每天会派发以下两种任务中的一个任务: 1.搜集情报:指派T号情报员搜集情报 2.传递情报:将一条情报从X号情报员传递 ...

  2. vue中引入vux

    1.安装相关依赖 cnpm install vux --save cnpm install vux-loader --save-dev cnpm install less less-loader -- ...

  3. UGUI小技巧之Text随文本内容自动变化大小

    看了网上很多帖子,都是说在 Text 上面加上 Content Size Fitter 组件,并将对应的轴向改成 Preferred size 就可以实现 Text 大小随着文本内容自适应,如下图: ...

  4. java面试一、1.4锁机制

    免责声明:     本文内容多来自网络文章,转载为个人收藏,分享知识,如有侵权,请联系博主进行删除. 1.3.锁机制 说说线程安全问题,什么是线程安全,如何保证线程安全 线程安全:当多个线程访问某一个 ...

  5. 【洛谷P2584】【ZJOI2006】GameZ游戏排名系统题解

    [洛谷P2584][ZJOI2006]GameZ游戏排名系统题解 题目链接 题意: GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在 ...

  6. N!中末尾有多少个0

    问题:先从100!的末尾有多少零         =>    再推广到  任意N!的末尾有多少个零 分析:首先想到慢慢求解出100!或N!,但计算机表示数有限,且要防止溢出. 则从数学上分析:一 ...

  7. easyui的下拉框combox动态复赋值显示在前端

    editbale:false设置为本输入框禁止编辑

  8. gitlab 之 项目管理

    首先git是什么? Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git的读音为/gɪt/. Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常 ...

  9. 包建强的培训课程(6):Android App瘦身优化

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  10. JavaScript的文档对象模型DOM

    小伙伴们之前我们讲过很多JavaScript的很多知识点,可以点击回顾一下: <JavaScript大厦之JS运算符>: <JavaScript工作原理:内存管理 + 如何处理4个常 ...