zoukankan      html  css  js  c++  java
  • 组合算法

    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);
        }
    
    }
  • 相关阅读:
    [背包]JZOJ 3232 【佛山市选2013】排列
    内核空间、用户空间、虚拟地址
    进程与线程的概念
    Python中字符串颜色
    socket编程
    模块与包
    常用模块
    面向对象进阶
    面向对象编程
    函数式编程
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15751966.html
Copyright © 2011-2022 走看看