leetcode — combinations
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的更多相关文章
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode——Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [leetcode]Combinations @ Python
原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...
- [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 ...
- [LeetCode] Combinations [38]
称号 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- LeetCode: Combinations 解题报告
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- [Leetcode] combinations 组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Combinations 回溯
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Combinations——递归
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- nacos 使用记
本文记录SpringBoot和SpringCloud与Nacos作为配置中心的整合过程及问题 Nacos官方使用文档:https://nacos.io/zh-cn/docs/what-is-nacos ...
- 《Linux就该这么学》第十四天课程
samba服务的配置文件解读 samba服务解决了Linux系统与Windows系统之间的文件共享问题,是一个非常不错的服务 原创地址:https://www.linuxprobe.com/chapt ...
- 关于Bell数的一道题目
考虑 T3+1 {1,2,3,4} T3是3个元素的划分,如果在里面加入子集{4}, 4被标成特殊元素, 就形成了T4一类的划分(里面的子集的并集是{1,2,3,4}) T2是2个元素的划 ...
- 如何安装ubuntu系统
https://www.cnblogs.com/Chinasf/archive/2010/05/06/1728840.html [Ubuntu 下挂ISO到虚拟光驱的方法] 各种方法参考如下论坛 ...
- 解决maven在build时下载文件卡死问题
1.停止build 2.cd ~/.m2/repository 3.在这个目录下找到你要下载的文件,然后查看是否有个同名文件带一个.lock后缀 4.rm -f xxxx.lock 5.重新bui ...
- bzoj1031(sa)
省选前练习模板系列: #include<iostream> #include<cstdio> #include<cmath> #include<cstring ...
- ubuntu16 gitlab的简单安装
1.安装好ubuntu的ssh服务,使用xshell登录虚拟机 2.下载安装包: wget -c https://downloads-packages.s3.amazonaws.com/ubuntu ...
- ES6中的箭头函数和普通函数有什么区别?
1.普通函数中的this总是指向调用它的那个对象, 箭头函数没有自己的this,他的this永远指向其定义环境,任何方法都改变不了其指向,如call().bind().apply().(正是因为它没有 ...
- maven 本地仓库无法更新到最新版本的jar包
maven 本地仓库无法更新到最新版本的jar包 描述:maven 本地仓库无法更新最新版的jar包导致项目一直报错 解决:去jar包版本所在目录,删除掉所有红框内文件,重新用ide导入
- La protezione del puntatore laser
Questo puntatore laser è sempre sufficientemente efficiente per eseguire il test più accurato su qua ...