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);
        }
    
    }
  • 相关阅读:
    qt中使用C++thread
    qt--mask蒙版
    qt--调色板QPalette
    qt--Q_PROPERTY自定义属性
    路由扩张器
    qt-QBitmap单色图
    qt-QPixmap
    nodejs中的文件系统
    深入理解nodejs中的异步编程
    Vue 事件监听
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15751966.html
Copyright © 2011-2022 走看看