从两个数组中找到总和组合

时间:2016-03-25 21:02:07

标签: java algorithm

有两个整数数组,即A = [2,9,8,13]和B = [5,3,1,11,4],

我们知道数组的长度, 我必须从数组A中选择一个整数,让我说我选择13,

现在我需要找出所有的组合 (数组A中的1个整数和数组B中的1个或1个以上的整数,例如从数组A和数组B中选择的所有整数之和为13或13的倍数)

阵列A中的

'2'和阵列B中的'11'使得2 + 11 = 13

数组A中的'p''8'和来自数组B的''1'和'4')使8 + 1 + 4 = 13

数组A中的'p''9'和来自数组B的''1'和'3')使9 + 1 + 3 = 13

来自数组A的

'13'和来自数组B的('1','3','4','5')使得13 + 1 + 3 + 4 + 5 = 26

注意: - 必须从数组A中只选择1个整数,并且可以从数组B中选择1个或多于1个整数

我发现的唯一方法就是在if else语句中写下所有组合,我绝对相信还有其他方式请帮助

2 个答案:

答案 0 :(得分:1)

我不知道这是否清楚,但这是我的解决方案:

public static void main(String[] args) {
    int[] A = new int[]{2,9,8,13};
    int[] B = new int[]{5,3,1,11,4};
    System.out.println(findMe(A, B));
}

private static List<List<Integer>> findMe(int[] A, int[] B) {

    List<List<Integer>> solutions = new ArrayList<List<Integer>>();
    Arrays.sort(A);
    if (A[A.length - 1] > 9) { //if the greatest is greater than 9
        for (int a1 : A) { //for all the values in A
            Collection<List<Integer>> solution = findMeNow(B, a1);
            solutions.addAll(solution);
        }
    }
    return solutions;
}

private static Collection<List<Integer>> findMeNow(int[] B, int a1) {
    List<List<Integer>> lists = new ArrayList<List<Integer>>(); //list of all possible combinations
    Set<List<Integer>> solutions = new HashSet<List<Integer>>(); //list of all possible combinations

    for (int b1 : B) {
        int currentSize = lists.size();
        for (int index = 0; index < currentSize; index++) {
            //for each sub list, create a copy, add the new element and add it to the mother list
            List<Integer> list = lists.get(index);
            List<Integer> copyList = new ArrayList<>(list);
            copyList.add(b1);
            lists.add(copyList); 
        }
        lists.add(new ArrayList<>(Arrays.asList(a1, b1)));

        //Then check the sum for each of the resulting lists
        for (List<Integer> list : lists) {
            int sum = 0;
            for (Integer value : list) {
                sum += value;
            }
            if (sum % 13 == 0) {
                solutions.add(list);
            }
        }
    }
    return solutions;
}

基本思想是&#34;广度优先搜索&#34;:您实际上正在构建一个包含数组B所有可能元素组合的树,例如:

  1. 5
  2. 5,3,[5,3]
  3. 5,3,[5,3],[5,1],[3,1],[5,3,1],1
  4. 等。等

    最糟糕的情况是,这相当于&#34;所有可能的组合&#34;就所需的操作数量而言,但平均来说它会更好。

    P.S。 :当你需要所有可能的解决方案时,这不再是广泛的第一次搜索了。

答案 1 :(得分:1)

这是一些伪代码:

  • 对数组进行排序
  • 从相对的两端开始工作(最高 - 最低 - 一半,最低 - 最高)
  • 使指针前进,以便找到所有组合

这给出了一个O(n log n)算法,它比O(n * n)强力“全组合”方法好得多。

它是O(n log n),因为排序是O(n log n),但最后一部分只是O(n)。

相关问题