组合算法
package org.example.permandcombine; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * @author xianzhe.ma * @date 2021/12/31 */ public class Combination { public static void main(String[] args) { List<Integer> input = new ArrayList<>(); input.add(1); input.add(2); input.add(3); input.add(4); combine(input,0,3,new LinkedList<>()); for (ArrayList list : result) { System.out.println(list.toString()); } } private static ArrayList<ArrayList<Integer>> result = new ArrayList<>(); public static void combine(List<Integer> input, int start, int length, LinkedList<Integer> tempList) { if (length == 0) { ArrayList<Integer> list = new ArrayList<>(); for (Integer num : tempList) { list.add(num); } result.add(list); return; } if (start == input.size()) { return; } tempList.add(input.get(start)); combine(input,start+1,length - 1,tempList); tempList.removeLast(); combine(input,start+1,length,tempList); } }