How do you rotate an array 90 degrees without using a storage array?

流过昼夜 提交于 2021-02-19 06:16:11

问题


I was instructed not to use a storage array to complete this task. Basically, we have to create a function that rotates the contents of a 2d array 90 degrees.

So if I start off with this array:

int[][] array = {{1,2,3}, {4,5,6}, {7,8,9}};

The function should return an array like this:

{{7,4,1}, {8,5,2}, {9,6,3}}

Again we are not allowed to use a created array within the function for storage. Is it even possible to accomplish this without a storage array?


回答1:


You can rotate/transpose the array by swapping the upper half with the lower half one by one:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        int[][] array = new int[][] {
            new int[] { 1, 2, 3},
            new int[] { 4, 5, 6},
            new int[] { 7, 8, 9},
        };

        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < row; col++) {
                int t = array[row][col];
                array[row][col] = array[col][row];
                array[col][row] = t;
            }
        }

        for (int row = 0; row < 3; row++) {
            System.out.println(Arrays.toString(array[row]));
        }
    }
}



回答2:


We can directly print a rotated matrix without saving a result as follows: first we iterate over the columns and then the rows, and print the points. Since we don't know beforehand the length of the row, i.e the number of columns, we iterate up to the Integer.MAX_VALUE and check at each step whether the columns are still present or not.

The rest of the algorithm is the same as when creating a transposed 2d array. The indices of points [i][j] become [j][i], but the indices of one of the sides becomes equal to the length of the side, minus the current index of the side, minus 1. Indexes start at 0.

int[][] array = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}};
for (int col = 0; col < Integer.MAX_VALUE; col++) {
    boolean max = true;
    for (int row = 0; row < array.length; row++) {
        if (col < array[row].length) {
    //      System.out.printf("%2d ", // transposed matrix
    //              array[row][col]);
    //      System.out.printf("%2d ", // rotated 90º clockwise ⟳
    //              array[array.length - row - 1][col]);
            System.out.printf("%2d ", // rotated 90º counterclockwise ⟲
                    array[row][array[row].length - col - 1]);
            max = false;
        }
    }
    System.out.println();
    if (max) break;
}

Output (rotated 90º counterclockwise ⟲):

 4  8 12 
 3  7 11 
 2  6 10 
 1  5  9 

See also:
• Filling a jagged 2d array first by columns
• The transpose of a matrix



来源:https://stackoverflow.com/questions/52619181/how-do-you-rotate-an-array-90-degrees-without-using-a-storage-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!