Return 来自给定数组的多维数组

Return multidimensional array from given array

我有以下数组:[1,2,3,4,5,6,7,8,9]

而且我必须return基于组和步骤参数以下值

例如:

组=3; 步骤 = 3;

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

组=3; 步骤 = 2;

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

组=3; 步骤 = 4;

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

到目前为止我有这个代码片段(在 Java 中):

public static String[][] arrayOfArrays(String[] arr, int step, int group) {
        int size = (arr.length / step) + 1;
        String[][] list = new String[size][group];
        int start = 0;
        for (int i = 0; i < size; i++) {
            for(int j = 0; j < group; j++) {
                list[i][j] = arr[start];
                start++;
            }
            if(start == arr.length) {
                start = 0;
            }
        }
        return list;
    }

我是算法的新手,我想了解我应该如何开始思考才能解决问题?

谢谢

在这里也没有太多经验,但我会尽力提供帮助,因为它看起来很有趣。 我使用了您的代码并将返回的数组更改为整数数组以匹配您的示例。

编辑: 感谢 @RoyceIrving 的回答,我理解了有多少内部数组背后的逻辑。我现在相信我的解决方案可以满足您的需求。


看看:

public static int[][] arrayOfArrays(int[] arr, int step, int group) {
    int size = (arr.length % step == 0) ? step + 1 : arr.length + 1;
    int[][] list = new int[size][group];
    for (int i = 0; i < size; i++) {
        int stepper = (step*i) % arr.length;
        for(int j = 0; j < group; j++) {
            list[i][j] = arr[stepper];
            stepper++;
            if (stepper == arr.length) {
                stepper = 0;
            }
        }
    }
    return list;
}

注意主要变化:

  • 大小取决于给定数组 (arr) 除以给定步长 (step)。如果是,则 size 等于 step + 1。否则,它等于数组长度 (arr.length ) + 1.
  • 变量“stepper”作为每个内部数组的正确起点(根据给定的“step”)。此变量用于将正确的数字添加到数组中,并在达到数组长度时重置为 0。添加了一个模数以防止其尺寸过大。

我想说的是,您首先要弄清楚的是您想要 return 得到的结果列表有多大。从上面的示例中我可以看出,您不断添加大小组列表,直到第一行与最后一行匹配。一旦我们知道了大小,那么我们就可以准确地创建一个大小合适的数组来填充我们的数据,然后return它。

public static int[][] arrayOfArrays(int[] arr, int step, int group) {
   int size = 0; // Keeps track of how many rows we got
   int c = 1; 
   // Figure out the size by counting by offset till we come back to starting with 1
   // Each time we count a full list add how many rows it would take 
   // to add that full list. Once we get back to starting with 1
   // Just add that final row
   while(true)
   {
       // Move number counter by how many indexes we will have moved in a list length
       c += step*(arr.length/group);

       // Because it wraps, lets do a modulo operation by the length of the array
       c %= arr.length;

       // Lets add how many rows we would have created this loop
       size += arr.length/group;

       if(c == 1) // Are we back to one as a first element in the row
       {
           size+=1; // Add one more for that final row
           break;
       }
   }
   // Now that we know size, lets make our array
   int[][] list = new int[size][group];

   // Stuff the array with data until we are done
   c = 1; // Reset our counter to 1
    for(int r = 0; r < size; r++)
    {
        for(int g = 0; g < group; g++)
        {
            list[r][g] = c;
            c = (c%arr.length) + 1; // Index and loop through the list
        }
    }
    return list;
}