【问题标题】:Split array into halves, quarters etc将数组分成两半、四分之一等
【发布时间】:2019-04-07 09:38:55
【问题描述】:

我正在寻找一种方法将数组分成两半(中间数字不包括在任何一半中),直到最后创建的数组只包含一个数字。中间的数字要么正好在中间(例如,对于 [1,2,3,4,5],它是 3)或者第二个的第一个数字数组的一半(对于 [1,2,3,4,5,6] 将是 4

假设我有一个数组:

[1,2,3,4,5,6,7,8,9,10]

我应该得到:

[1,2,3,4,5] [7,8,9,10]

[1,2] [4,5] [7,8] [10]

[2]    [5]   [8]

我尝试了递归,但它总是只分割数组的一半

public int[][] divide(int[] values, int[][] split_values, int index) {
  int root_index = 0;
  if(values.length > 1) {
    if(values.length % 2 != 0) {
        root_index = (values.length - 1) / 2;
    }
    else {
        root_index = (int) ((values.length) / 2);
    }
    int[] left_value = new int[root_index-1];
    int[] right_value = new int[values.length - left_value.length - 1];
    for(int i = 0; i < values.length; i++) {
        if(i < left_value.length) {
            left_value[i] = values[i];
        }
        else {
            right_value[i] = values[i];
        }
    }

    split_values[index] = left_value;
    index++;
    split_values[index] = right_value;
    index++;
  }
  for(int[] value : split_values) {
        divide(value, split_values, index);
  }
  return split_values;
}

编辑:所以我改变了一点,函数现在接受参数

 int[] values, int[][] split_values, String used, int index
 ...
 if(values.length > 1) {
 ...
 used += "!" + Arrays.toString(values) + "!";
 }

     for(int[] value : split_values) {
    if(!used.contains("!" + Arrays.toString(value) + "!")) {
            divide(value, split_values, used, index);
    }        
}

现在的问题是,这是一个永无止境的循环。

【问题讨论】:

  • 你试过什么?这不是免费的代码编写服务。
  • 编辑了我的评论
  • 你有没有尝试过?你到底卡在哪里了?
  • 我想我越来越近了。我的函数现在看起来像这样: public int[][] divide(int[] values, int[][] split_values, String used, int index) { 我将所有划分的数组放入字符串“用过的”。现在的问题是程序是一个永无止境的循环。

标签: java list split


【解决方案1】:

也许这会有所帮助:

保留所有元素:

 public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        splitTo(array);
    }

    static void splitTo(Integer[] source) {
        int length = source.length;
        if (length == 1) return;
        boolean odd = length % 2 != 0;
        int half1 = length / 2;
        int half2 = odd ? half1 + 1 : half1;
        Integer[] one = new Integer[half1];
        Integer[] two = new Integer[half2];
        int index = 0;
        for (Integer element : one) {
            one[index] = source[index];
            two[index] = source[half1 + index];
            index++;
        }
        if (odd) two[index] = source[half1 + index];
        Integer[][] integers = {one, two};
        System.out.println(Arrays.deepToString(integers));
        splitTo(one);
        splitTo(two);
    }

拒绝中间人:


    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        splitTo(array);
    }

    static void splitTo(Integer[] source) {
        int length = source.length;
        if (length == 1) return;
        boolean odd = length % 2 != 0;
        int half = length / 2;
        Integer[] one = new Integer[half];
        Integer[] two = new Integer[half];
        int index = 0;
        for (Integer element : one) {
            one[index] = source[index];
            two[index] = odd ? source[half + index + 1] : source[half + index];
            index++;
        }
        Integer[][] integers = {one, two};
        System.out.println(Arrays.deepToString(integers));
        splitTo(one);
        splitTo(two);
    }

如果你学习 MergeSort 算法,那么好的做法是 https://en.wikipedia.org/wiki/Merge_sort

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2013-05-25
    • 2013-12-06
    相关资源
    最近更新 更多