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. (PMP)第5章-----项目范围管理

    产品范围:所具有的特征和功能 项目范围:必须完成的工作. 5.1 规划范围管理 输入 工具与技术 输出 1.项目章程 2.项目管理计划 (质量管理计划, 项目生命周期描述, 开发方法) 3.事业环境因 ...

  2. temp--贵州银行

    -------住宿----泊乐酒店----8905----与朱聿一起住 2018年  1月3日晚 1月4日晚  1月5日晚 1月6日晚  1月7日晚 1月8日晚  1月9日晚 已结清! ======= ...

  3. 第52章:Java操作MongoDB-[Mongo-Java-3.x]

    ①范例:连接数据库 package cn.mldn; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import ...

  4. [小结]InnoDB体系结构及工作原理

    参阅:<innodb存储引擎内幕>原创文章,会不定时更新,转发请标明出处:http://www.cnblogs.com/janehoo/p/7717041.html 一.概述: innod ...

  5. 感知机、logistic回归 损失函数对比探讨

    感知机.logistic回归 损失函数对比探讨 感知机 假如数据集是线性可分的,感知机学习的目标是求得一个能够将正负样本完全分开的分隔超平面 \(wx+b=0\) .其学习策略为,定义(经验)损失函数 ...

  6. one-to-one 一对一映射关系(转 wq群)

    2.配置对应的xml配置文件 person的配置文件 idCard的配置文件 idCard的配置文件  3.测试 运行测试程序后,控制台输出两条语句

  7. vue数据更新UI不刷新显示解决方案

    vue比较常见的坑就是数据(后台返回)更新了,但是UI界面并没有更新,常见于以下情况: 一.数据为数组时1.通过数组索引修改数组元素例如: 此时UI数据并不会刷新 2.修改数组长度时: 解决方案: 如 ...

  8. 简析 __init__、__new__、__call__ 方法

    简析 __init__.__new__.__call__ 方法 任何事物都有一个从创建,被使用,再到消亡的过程,在程序语言面向对象编程模型中,对象也有相似的命运:创建.初始化.使 用.垃圾回收,不同的 ...

  9. Slot使用

    1.组件中有单个或多个未命名slot标签时,如下:<Child><span style=”color:red;”>hello world</span></Ch ...

  10. RabbitMQ 使用主题进行消息分发

    在上篇文章RabbitMQ消息队列(五):Routing 消息路由 中,我们实现了一个简单的日志系统.Consumer可以监听不同severity的log.但是,这也是它之所以叫做简单日志系统的原因, ...