标签:list 一个 java代码 integer put get from 用例 import
列出所有组合,例如从1,2,3,4中任取2个元素,求出所有的组合。
面对combinations([1,2,3,4], 2):
递归思路为:
不选1 → combinations([2,3,4], 2)
/**
* Generates all combinations and output them,
* selecting n elements from data
*/
public void combinations(List<Integer> data, int n) {
// initial value for recursion
if (data.isEmpty()) {
if (n == 0) {
// output empty list
}
return;
}
if (n < 0) {
return;
}
if (n == 0) {
// output empty list
return;
}
// select element 0
combinations(data.subList(1, data.size()), n - 1);
// un-select element 0
combinations(data.subList(1, data.size()), n);
}
合并初始值条件后,考虑两个问题:
如何选择元素?如何输出?
/**
* Generates all combinations and output them,
* selecting n elements from data
*/
public void combinations(List<Integer> data, int n) {
// how to select elements
// how to output
if (n == 0) {
// output all selected elements
return;
}
if (data.isEmpty()) {
// output empty list
return;
}
// select element 0
combinations(data.subList(1, data.size()), n - 1);
// un-select element 0
combinations(data.subList(1, data.size()), n);
}
维护一个selected,也就是之前所选择的的所有元素。
/**
* Generates all combinations and output them,
* selecting n elements from data
*/
public void combinations(
List<Integer> selected, List<Integer> data, int n) {
if (n == 0) {
// output all selected elements
for (Integer i : selected) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
return;
}
if (data.isEmpty()) {
// output empty list
return;
}
// select element 0
selected.add(data.get(0));
combinations(selected, data.subList(1, data.size()), n - 1);
// un-select element 0
selected.remove(selected.size() - 1);
combinations(selected, data.subList(1, data.size()), n);
}
测试程序是否正确运行:
public static void main(String[] args) {
Combinations comb = new Combinations();
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 2);
}
运行结果为
多测几个极端用例
public static void main(String[] args) {
Combinations comb = new Combinations();
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 2);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(), 2);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(), 0);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 1);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 0);
System.out.println("=================");
}
运行结果为
全部代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Combinations {
/**
* Generates all combinations and output them,
* selecting n elements from data
*/
public void combinations(
List<Integer> selected, List<Integer> data, int n) {
if (n == 0) {
// output all selected elements
for (Integer i : selected) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
return;
}
if (data.isEmpty()) {
// output empty list
return;
}
// select element 0
selected.add(data.get(0));
combinations(selected, data.subList(1, data.size()), n - 1);
// un-select element 0
selected.remove(selected.size() - 1);
combinations(selected, data.subList(1, data.size()), n);
}
public static void main(String[] args) {
Combinations comb = new Combinations();
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 2);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(), 2);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(), 0);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 1);
System.out.println("=================");
comb.combinations(
new ArrayList<>(), Arrays.asList(1, 2, 3, 4), 0);
System.out.println("=================");
}
}
标签:list 一个 java代码 integer put get from 用例 import
原文地址:https://www.cnblogs.com/PyLearn/p/10037060.html