多个数组间元素排列组合问题求解(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);

    }



  • 2017-08-08 11:17:17

    nginx 反向代理 取得真实IP和域名

    nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递到应用程序中。

  • 2017-08-09 15:14:52

    如何写好.babelrc?Babel的presets和plugins配置解析

    官网是这么说的,翻译一下就是下一代JavaScript 语法的编译器。 作为前端开发,由于浏览器的版本和兼容性问题,很多JavaScript的新的方法都不能使用,等到可以大胆使用的时候,可能已经过去了好几年。Babel就因此而生,它可以让你放心使用大部分的JavaScript的新的标准的方法,然后编译成兼容绝大多数的主流浏览器的代码。

  • 2017-08-15 17:44:21

    glob 介绍

    glob 最早是出现在类Unix系统的命令行中, 是用来匹配文件路径的。比如,lib/**/*.js 匹配 lib 目录下所有的 js 文件。 除了在命令行中,我们在程序中也会有匹配文件路径的需求。于是,很多编程语言有了对 glob 的实现 ,如 Python 中的 glob 模块; php 中的 glob 方法。

  • 2017-08-16 08:45:41

    nodejs中流(stream)的理解

    这种方式是把文件内容全部读入内存,然后再写入文件,对于小型的文本文件,这没有多大问题,比如grunt-file-copy就是这样实现的。但是对于体积较大的二进制文件,比如音频、视频文件,动辄几个GB大小,如果使用这种方法,很容易使内存“爆仓”。理想的方法应该是读一部分,写一部分,不管文件有多大,只要时间允许,总会处理完成,这里就需要用到流的概念。

  • 2017-08-17 17:58:48

    /usr、/home、/bin、/dev、/var、/etc中主要存放什么文件

    /usr 最庞大的目录,要用到的应用程序和文件几乎都在这个目录。其中包含: /usr/X11R6 存放X window的目录 /usr/bin 众多的应用程序 /usr/sbin 超级用户的一些管理程序 /usr/doc linux文档 /usr/include linux下开发和编译应用程序所需要的头文件 /usr/lib 常用的动态链接库和软件包的配置文件 /usr/man 帮助文档 /usr/src 源代码,linux内核的源代码就放在/usr/src/linux里 /usr/local/bin 本地增加的命令 /usr/local/lib 本地增加的库