若干个集合中元素的组合问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ymaini/article/details/82287393

题目

给定若干个字符串组成的集合,获取集合之间元素全部组合。

例:

集合1: [“a”, “b”]

集合2: [“A”, “B”]

全部组合: [“aA”, “aB”, “bA”, “bB”]

解题思路

利用数学上的笛卡尔积的思想实现两个集合元素的组合。

对于若干个集合,依次处理当前集合和下一个集合,将获得的结果作为新集合,再与下下个集合进行组合。

为此我们实现2个函数:

Function1: 实现2个集合元素的组合
Function2: 实现若干个集合的组合

代码实现

实现过程中为例验证集合的有效性,我们添加了一个集合验证函数

Function3: 验证集合的有效性

具体实现:

package hello.core.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created on 2018/9/1.
 *
 * @author Marvin Yang
 */
public class CollectionCombination {

    /**
     * 若干个集合元素的组合
     *
     * @param groups 多个集合
     * @return 组合结果
     */
    public static List<String> combination(List<List<String>> groups) {
        if (invalid(groups) || invalid(groups.get(0))) {
            return null;
        }
        List<String> combine = groups.get(0);
        for (int i = 1; i < groups.size(); i++) {
            combine = cartesianProduct(combine, groups.get(i));
            if (combine == null) {
                return null;
            }
        }
        return combine;
    }

    /**
     * 两个集合元素的组合
     *
     * @param c1 集合1
     * @param c2 集合2
     * @return 组合结果
     */
    public static List<String> cartesianProduct(List<String> c1, List<String> c2) {
        if (invalid(c1) || invalid(c2)) {
            return null;
        }
        List<String> combine = new ArrayList<>();
        for (String s : c1) {
            for (String t : c2) {
                combine.add(String.format("%s%s", s, t));
                //combine.add(String.format("%s%s", t, s));
            }
        }
        return combine;
    }

    /**
     * 验证集合是否无效
     *
     * @param c 集合
     * @return true 无效
     */
    private static boolean invalid(List<?> c) {
        return c == null || c.isEmpty();
    }
}

构造测试样例进行单测,测试样例如下:

package hello.core.test;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created on 2018/9/1.
 *
 * @author Marvin Yang
 */
public class CollectionCombinationTest {

    @Test
    public void combination() {
        List<String> c1 = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
        List<String> c2 = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));
        List<String> c3 = new ArrayList<>(Arrays.asList("I", "II", "III", "IV"));
        List<List<String>> collections = new ArrayList<>();
        collections.add(c1);
        collections.add(c2);
        collections.add(c3);
        List<String> combine = CollectionCombination.combination(collections);
        System.out.println(combine);
        System.out.println("actual count: " + (combine == null? 0:combine.size()));
    }

    @Test
    public void cartesianProduct() {
        List<String> c1 = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
        List<String> c2 = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));
        List<String> test = CollectionCombination.cartesianProduct(c1, c2);
        System.out.println(test);
    }
}

题目变形

简单修改一下题目,给出2种题目变形。

变形1

要求组合结果不能出现重复的情况。

思路:

由于要求不能出现重复的情况,可以修改组合的数据结构,有List 修改为Set。

变形2

要求组合结果不考虑集合之间的顺序,原题目中的组合结果,集合1的元素排在前面,集合2的元素排在后面。

思路:

修改函数cartesianProduct(),再添加一种组合方式,将我们函数中注释掉的部分添加即可。

TODO

文本实现的方法比较简单,尽管可以实现功能,但是对性能本身没有考虑。

为此需要考虑是否还有更好的方法,不需要遍历那么多次。

猜你喜欢

转载自blog.csdn.net/ymaini/article/details/82287393