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

    }



  • 2020-12-16 22:04:03

    基于本地代理的边下边播技术分析

    我们熟知的边下边播技术,是迅雷提供的,还有之前的快播、快车等工具,它们使用的技术基本上都是P2P下载技术。 P2P下载技术,本质上它并不是C-S的架构,P2P----> Peer to Peer,实际上它将各个客户端的资源调度起来,给上传资源种子,方便后续的下载者可以快速有效的下载资源,这种方式需要服务器整合各个Client,在有用户需要下载的情况下,服务器能及时调度资源,开始给下载者提供资源信息,保证下载者下载资源越快越好。P2P的下载方式后面我们专门介绍一下。这儿不继续展开了。

  • 2020-12-16 22:07:44

    Android视频点播-边播边缓存

    一些知名的视频app客户端(优酷,爱奇艺)播放视频的时候都有一些缓存进度(二级进度缓存),qq,微信有关的小视频,还有一些短视频app,都有边播边缓的处理。还有就是当文件缓存完毕了再次播放的话就不再请求网络了直接播放本地文件了。既节省了流程又提高了加载速度。 今天我们就是来研究讨论实现这个边播边缓存的框架,因为它不和任何的业务逻辑耦合。

  • 2020-12-16 22:46:44

    基于coturn项目的stun/turn服务器搭建

    webrtc是google推出的基于浏览器的实时语音-视频通讯架构。其典型的应用场景为:浏览器之间端到端(p2p)实时视频对话,但由于网络环境的复杂性(比如:路由器/交换机/防火墙等),浏览器与浏览器很多时候无法建立p2p连接,只能通过公网上的中继服务器(也就是所谓的turn服务器)中转。示例图如下:

  • 2020-12-16 23:06:05

    Rocket.Chat推送信息

    Rocket.Chat推送消息 Rocket.Chat是一个开源实时通讯平台, 支持Windows, Mac OS, Linux. 支持聊天, 文件上传, 视频通话, 语音通话功能. 向Rocket.Chat推送消息 以下示例可以转为别的语言的版本, 本示例使用Linux平台的curl测试, curl非常强大. 登陆 首先需要登陆Rocket.Chat服务器

  • 2020-12-17 09:01:23

    对BitTorrent Tracker源码分析

    tracker服务器是BT下载中必须的角色。一个BT client 在下载开始以及下载进行的过程中,要不停的与 tracker 服务器进行通信,以报告自己的信息,并获取其它下载client的信息。这种通信是通过 HTTP 协议进行的,又被称为 tracker HTTP 协议,它的过程是这样的: client 向 tracker 发一个HTTP 的GET请求,并把它自己的信息放在GET的参数中;这个请求的大致意思是:我是xxx(一个唯一的id),我想下载yyy文件,我的ip是aaa,我用的端口是bbb。。。

  • 2020-12-17 10:55:48

    html5 video p2p research

    节约带宽,减少缓冲时间,提升服务质量,处理峰值流量, 视频观看的人越多,播放越流畅。

  • 2020-12-17 10:57:34

    使用 MediaSource 搭建流式播放器

    Media Source Extensions(媒体源扩展)大大地扩展了浏览器的媒体播放功能,提供允许JavaScript 生成媒体流。这可以用于自适应流(adaptive streaming,也是我毕设的研究方向)及随时间变化的视频直播流(live streaming)等应用场景。