打印没有元素相邻的所有子序列数组

时间:2019-05-11 06:38:50

标签: java algorithm

在下面,我可以打印所有子序列数组,但是有人可以帮助打印所有没有相邻元素的子序列。

`package test;

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

public class Test1 {
    public static List<List<Integer>> combinations(int[] arr) {
        List<List<Integer>> combinations = new ArrayList<List<Integer>>();
        List<Integer> total;
        for (int i = 0; i < arr.length; i++) {
            int k = combinations.size();
            for (int j = 0; j < k; j++) {
                // if ((i + 1 < arr.length)) {

                total = new ArrayList<Integer>(combinations.get(j));
                total.add(new Integer(arr[i]));

                combinations.add(total);
                // }
            }
            total = new ArrayList<Integer>();
            total.add(new Integer(arr[i]));
            combinations.add(total);
        }
        System.out.println(combinations);
        return combinations;
    }

    public static void main(String args[]) {
        int arr[] = { 1, 2, 3 };
        combinations(arr);
    }
}`

输出:-[[1],[1、2],[2],[1、3],[1、2、3],[2、3],[3]]

预期输出:-[[1],[2],[1、3],[3]]

2 个答案:

答案 0 :(得分:1)

我决定将解决方案分为两种方法,一种是简单的方法,可以从数组中为给定的起始位置和步长创建一个List,另一种是主要方法,它针对所有可能的组合调用第一个方法。

static List<Integer> oneLoop(int[] arr, boolean isOdd, int step) {
    int start = isOdd ? 1 : 0;
    List<Integer> result = new ArrayList<>();
    for (int i = start; i < arr.length; i += step) {
        result.add(arr[i]);    
    }
    return result;
}

static List<List<Integer>> combinations(int[] arr) {
    List<List<Integer>> allCombinations = new ArrayList<>();

    //Add each single element as separate list
    for (int i = 0; i < arr.length; i++) {
        List<Integer> list = new ArrayList<>();
        list.add(arr[i]);
        allCombinations.add(list);
    }
    //Loop over an increasing larger step size until size is to large
    int step = 2;
    while (step < arr.length) {                        
        allCombinations.add(oneLoop(arr, false, step));
        if ( (step +1) < arr.length) {
            allCombinations.add(oneLoop(arr, true, step));
        }
        step += 1;
    }

    return allCombinations;
}

这是我的测试方法

public static void main(String[] args) {
    int[] array = new int[args.length];
    for (int i = 0; i < args.length; i++) {
        array[i] = Integer.valueOf(args[i]);
    } 
    List<List<Integer>> list = combinations(array);
    System.out.println(list);
}

答案 1 :(得分:1)

生成组合:

private static List<List<Integer>> generateCombinations(int[] arr){
        List<List<Integer>> combs = new ArrayList<List<Integer>>();
        int prev2 = 1,prev1 = 1;

        for(int i=0;i<arr.length;++i){
             //individual
            List<Integer> l = new ArrayList<>();
            l.add(arr[i]);
            combs.add(l);   

            if(i < 2){ // since these are base cases for our fibonacci sequence
                continue;
            }

            int size = prev1 + prev2 - 1; 

            for(int j=0;j<size;++j){
                List<Integer> new_list = new ArrayList<>(combs.get(j));
                new_list.add(arr[i]);
                combs.add(new_list);
            }           

            prev1 = prev1 + prev2;
            prev2 = prev1 - prev2;
        }        

        return combs;
    }

驱动程序代码:

public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        List<List<Integer>> result = generateCombinations(arr);
        int size = result.size();
        for(int i=0;i<size;++i){
            System.out.println(result.get(i).toString());
        }
    }

输出:

[1]
[2]
[3]
[1, 3]
[4]
[1, 4]
[2, 4]
[5]
[1, 5]
[2, 5]
[3, 5]
[1, 3, 5]

算法:

  • 生成避免相邻元素的组合实际上遵循斐波那契序列。

让我们将数组设为{1,2,3,4,5,6}

+-------------------------------------------------------------------------+---+---+---+---+---+---+
|                                                                         |   |   |   |   |   |   |
+-------------------------------------------------------------------------+---+---+---+---+---+---+
| Individual element's generated combinations excluding adjacent elements | 1 | 1 | 2 | 3 | 5 | 8 |
+-------------------------------------------------------------------------+---+---+---+---+---+---+
| Elements of the array                                                   | 1 | 2 | 3 | 4 | 5 | 6 |
+-------------------------------------------------------------------------+---+---+---+---+---+---+
  • 换句话说,这意味着我将从头开始迭代所有组合,并在可能生成的组合总数-1(其中-1本身不包括在内)的位置进行迭代。

  • 让我们仔细研究一下这些组合以提高清晰度。假设前两个组合已经存在。

  

[1]

     

[2]

  • 因此,对于3,我们将从头开始,
  

[1,3]

     

[3](我们会添加自己)

  • 对于4,我们面前会出现所有组合:
  

[1]

     

[2]

     

[1,3]

     

[3]

  • 现在,我们只走到[2]然后停下来,进行如下组合:
  

[1,4]

     

[2,4]

     

[4](自己添加)。

  • ,以此类推。希望这可以帮助。