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 ...
随机推荐
- Jquery.Datatable 控件后端分页实例 (后台使用ashx、aspx-webmethod)
本实例引用Datatable版本号: 1.10.16 一.传到aspx后台(webmethod) 1.添加js.css引用: <link href="/Scripts/ThirdLib ...
- java操作docker示例(docker-java)
1.首先要修改docker服务器的 /usr/lib/systemd/system/docker.service,加入紫色框的配置 2.下载docker-java 的github源码 git clon ...
- 微信小程序拉起登录的操作
第一步,前端调用wx.login()接口把token数据请求过来, 第二部,把tok嗯发送到总计的服务器,然后进行微信openid和assession的获取 第三部验证session是否过期,过期重新 ...
- 排序算法(sorting algorithm) 之 选择排序(selection sort)
https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...
- windows putty xming virt-manager
记一次windows环境使用linux下使用virt-manager软件的问题 环境:windows server 2008.ubuntu-server 软件:putty.virt-manager.x ...
- pom.xml文件模板、application文件模板、configuration逆向生成文件、
pom: <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:// ...
- eclipse 导入包含子maven项目的maven项目时的正确方式(父子项目)
eclipse 导入包含子maven项目的maven项目时的正确方式(父子项目) NO1 导入时依次选择 import > Maven > Existing Maven Projects ...
- Django开发目录
Django开发[第一章]:Django基础和基本使用 Django开发[第二章]:Django URLConf 进阶 Django开发[第三章]:Django View 进阶 Django开发[第四 ...
- 网易彩票-我的彩票-设置-cell跳转界面
1. 点击“cell”推出对应的界面 1.1 新建group,名为:Setting 路径:MYLottery(我的彩票)->Controller 1.2 新建Cocoa Touch Class, ...
- Openvswitch手册(8): ovs-vsctl的DB的操作
ovs-vsctl的DB的操作 如果你在命令行里面找不到相应的命令创建和删除对象,则可以直接删除数据库 [−−if−exists] [−−columns=column[,column]...] lis ...