多个数组间元素排列组合问题求解(Java实现),可用于商品规格

2019-09-18 22:21:43

参考地址 多个数组间元素排列组合问题求解(Java实现)

以前还整理过一个js的,下次读到了可以写过来。


求多个数组之间元素的排列组合问题,方法有两个:递归法、循环法。

首先应该认识到的是:


所有可以用递归实现的操作都可以转化为用while、for等循环实现。

递归法

优缺点:

数组数量不太多时用递归法确实使程序比较简洁,数组数量太多时递归函数栈过大,有可能导致运行时栈溢出。而且相对常用的算法如普通循环等,运行效率较低。


实现代码一

/**

     * 写法一,递归计算所有组合

     * @param inputList 所有数组的列表

     * */

    public List<String> combination(List<String> inputList){

        List<String> resList = new ArrayList<>();

        combinationInt(inputList, resList, 0, 

                new char[inputList.size()]);

        return resList;

    }


    private void combinationInt(List<String> inputList, List<String> resList,

            int ind, char[] arr) {

        if(ind == inputList.size()){

            resList.add(new String(arr));

            return;

        }

        for(char c: inputList.get(ind).toCharArray()){

            arr[ind] = c;

            combinationInt(inputList, resList, ind + 1, arr);

        } 

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

测试程序

//测试部分

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();

        list.add("ab");

        list.add("12");

        list.add("AB");

        List<String> result=combination(list);

        System.out.println("组合结果:");

        for (String string : result) {

            System.out.println(string);

        }   

    }

1

2

3

4

5

6

7

8

9

10

11

12

实现代码二

/**

     * 写法二,递归计算所有组合

     * @param inputList 所有数组的列表,数组用List<Integer>存储

     * @param beginIndex 代表每一个数组的在inputList中的索引

     * @param arr 用于保存每一次递归生成的组合

     * */

    public void calculateCombination(List<List<Integer>> inputList, int beginIndex, int[] arr) {

        if(beginIndex == inputList.size()){

            //在这里进行你自己的处理,比如打印组合的结果

            for (int i : arr) {

                System.out.print(i+", ");

            }

            System.out.println();

            return;

        }

        for(int c: inputList.get(beginIndex)){

            arr[beginIndex] = c;

            calculateCombination(inputList, beginIndex + 1, arr);

        }

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

测试程序

//测试部分

    public static void main(String[] args) {

        List<Integer> list1 = new ArrayList<>();

        list1.add(0);

        list1.add(1);

        list1.add(2);

        List<Integer> list2 = new ArrayList<>();

        list2.add(3);

        list2.add(4);

        list2.add(5);

        List<Integer> list3 = new ArrayList<>();

        list3.add(6);

        list3.add(7);

        list3.add(8);

        List<List<Integer>> allList = new ArrayList<>();

        allList.add(list1);

        allList.add(list2);

        allList.add(list3);

        calculateCombination(allList, 0, new int[allList.size()]);  

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

循环法

优缺点:

采用循环方式程序编写较递归法略复杂,但是执行效率高,不管数组数量多少,都不会发生栈溢出。


代码

/**

     * 算法二,非递归计算所有组合

     * @param inputList 所有数组的列表

     * */

    public void calculateCombination(List<List<Integer>> inputList) {

        List<Integer> combination = new ArrayList<Integer>();

        int n=inputList.size();

        for (int i = 0; i < n; i++) {

            combination.add(0);

        }

        int i=0;

        boolean isContinue=false;

        do{

        //打印一次循环生成的组合

            for (int j = 0; j < n; j++) {

                System.out.print(inputList.get(j).get(combination.get(j))+", ");

            }

            System.out.println();


            i++;

            combination.set(n-1, i);

            for (int j = n-1; j >= 0; j--) {

                if (combination.get(j)>=inputList.get(j).size()) {

                    combination.set(j, 0);

                    i=0;

                    if (j-1>=0) {

                        combination.set(j-1, combination.get(j-1)+1);

                    }

                }

            }

            isContinue=false;

            for (Integer integer : combination) {

                if (integer != 0) {

                    isContinue=true;

                }

            }

        }while (isContinue);

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

测试程序

//测试部分

    public static void main(String[] args) {

        List<Integer> list1 = new ArrayList<>();

        list1.add(0);

        list1.add(1);

        list1.add(2);

        List<Integer> list2 = new ArrayList<>();

        list2.add(3);

        list2.add(4);

        list2.add(5);

        List<Integer> list3 = new ArrayList<>();

        list3.add(6);

        list3.add(7);

        list3.add(8);

        List<List<Integer>> allList = new ArrayList<>();

        allList.add(list1);

        allList.add(list2);

        allList.add(list3);

        calculateCombination(allList);

    }



  • 2021-02-03 17:01:30

    iOS库 .a与.framework区别

    静态库:连接时完整地拷贝至可执行文件中,被屡次使用就有多份冗余拷贝。 动态库:连接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次,多个程序共用,节省内存。

  • 2021-02-03 17:13:58

    iOS - 封装静态库

    静态库:链接时完整的拷贝至可执行文件中,被多次使用就有多份冗余拷贝,.a的静态库 .framework的静态库

  • 2021-02-03 17:16:07

    iOS 中的动态库、静态库和 framework

    首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用。 什么时候我们会用到库呢?一种情况是某些代码需要给别人使用,但是我们不希望别人看到源码,就需要以库的形式进行封装,只暴露出头文件。另外一种情况是,对于某些不会进行大的改动的代码,我们想减少编译的时间,就可以把它打包成库,因为库是已经编译好的二进制了,编译的时候只需要 Link 一下,不会浪费编译时间。

  • 2021-02-03 17:17:53

    iOS 同一个工程下打包不同的app

    应用图标,启动画面,应用启动后的首页都不一样。 有些课程(例如公务员考试和高考)是有目标考试的概念,不同的目标考试大纲是不一样的。拿高考来举例,北京的高考和上海的高考,就有着完全不一样的考试大纲。高考的文科和理科,又有着完全不同的考试科目。 有些课程会有一些自定义的界面,例如高考的应用可以设置昵称,有些课程的真题练习中是有推荐真题模块的,而有些课程又没有。 有些课程有扫描答题卡功能,有些课程有考前冲刺功能,有些课程有大题专项查看功能,而有些课程又没有上述功能。另外还有一些微小细节,但是解决方法和类似,所以就不一一展开说明。

  • 2021-02-04 14:02:30

    window软件界面找不到了跑到屏幕外面去了

    一般可以这样操作,按Alt+空格,然后按M,然后用上下左右键把窗口移动到能看到的地方,再按回车。有些第三方的软件可能不能用,大部分都可以这样做。

  • 2021-02-04 14:08:13

    基于 Electron 的爬虫框架 Nightmare

    Electron 可以让你使用纯 JavaScript 调用 Chrome 丰富的原生的接口来创造桌面应用。你可以把它看作一个专注于桌面应用的 Node.js 的变体,而不是 Web 服务器。其基于浏览器的应用方式可以极方便的做各种响应式的交互,接下来介绍下关于 Electron 上衍生出的框架 Nightmare

  • 2021-02-04 20:13:02

    iOS framework制作及使用(包含xib和图片)

    静态库与动态库简介: 静态库:链接使用时完整地拷贝至可执行文件中 动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次 本文制作framework对应xcode版本:10.1