How do you rotate a two dimensional array?

后端 未结 30 3062
耶瑟儿~
耶瑟儿~ 2020-11-22 02:43

Inspired by Raymond Chen\'s post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I\'d

相关标签:
30条回答
  • 2020-11-22 03:34

    Here's my Ruby version (note the values aren't displayed the same, but it still rotates as described).

    def rotate(matrix)
      result = []
      4.times { |x|
        result[x] = []
        4.times { |y|
          result[x][y] = matrix[y][3 - x]
        }
      }
    
      result
    end
    
    matrix = []
    matrix[0] = [1,2,3,4]
    matrix[1] = [5,6,7,8]
    matrix[2] = [9,0,1,2]
    matrix[3] = [3,4,5,6]
    
    def print_matrix(matrix)
      4.times { |y|
        4.times { |x|
          print "#{matrix[x][y]} "
        }
        puts ""
      }
    end
    
    print_matrix(matrix)
    puts ""
    print_matrix(rotate(matrix))
    

    The output:

    1 5 9 3 
    2 6 0 4 
    3 7 1 5 
    4 8 2 6 
    
    4 3 2 1 
    8 7 6 5 
    2 1 0 9 
    6 5 4 3
    
    0 讨论(0)
  • 2020-11-22 03:34

    For i:= 0 to X do For j := 0 to X do graphic[j][i] := graphic2[X-i][j]

    X is the size of the array the graphic is in.

    0 讨论(0)
  • 2020-11-22 03:35

    Here is one that does the rotation in place instead of using a completely new array to hold the result. I've left off initialization of the array and printing it out. This only works for square arrays but they can be of any size. Memory overhead is equal to the size of one element of the array so you can do the rotation of as large an array as you want.

    int a[4][4];
    int n = 4;
    int tmp;
    for (int i = 0; i < n / 2; i++)
    {
        for (int j = i; j < n - i - 1; j++)
        {
            tmp             = a[i][j];
            a[i][j]         = a[j][n-i-1];
            a[j][n-i-1]     = a[n-i-1][n-j-1];
            a[n-i-1][n-j-1] = a[n-j-1][i];
            a[n-j-1][i]     = tmp;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:37

    Here it is in C#

    int[,] array = new int[4,4] {
        { 1,2,3,4 },
        { 5,6,7,8 },
        { 9,0,1,2 },
        { 3,4,5,6 }
    };
    
    int[,] rotated = RotateMatrix(array, 4);
    
    static int[,] RotateMatrix(int[,] matrix, int n) {
        int[,] ret = new int[n, n];
    
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                ret[i, j] = matrix[n - j - 1, i];
            }
        }
    
        return ret;
    }
    
    0 讨论(0)
  • 2020-11-22 03:38

    here's a in-space rotate method, by java, only for square. for non-square 2d array, you will have to create new array anyway.

    private void rotateInSpace(int[][] arr) {
        int z = arr.length;
        for (int i = 0; i < z / 2; i++) {
            for (int j = 0; j < (z / 2 + z % 2); j++) {
                int x = i, y = j;
                int temp = arr[x][y];
                for (int k = 0; k < 4; k++) {
                    int temptemp = arr[y][z - x - 1];
                    arr[y][z - x - 1] = temp;
                    temp = temptemp;
    
                    int tempX = y;
                    y = z - x - 1;
                    x = tempX;
                }
            }
        }
    }
    

    code to rotate any size 2d array by creating new array:

    private int[][] rotate(int[][] arr) {
        int width = arr[0].length;
        int depth = arr.length;
        int[][] re = new int[width][depth];
        for (int i = 0; i < depth; i++) {
            for (int j = 0; j < width; j++) {
                re[j][depth - i - 1] = arr[i][j];
            }
        }
        return re;
    }
    
    0 讨论(0)
  • 2020-11-22 03:39

    Here is my attempt for matrix 90 deg rotation which is a 2 step solution in C. First transpose the matrix in place and then swap the cols.

    #define ROWS        5
    #define COLS        5
    
    void print_matrix_b(int B[][COLS], int rows, int cols) 
    {
        for (int i = 0; i <= rows; i++) {
            for (int j = 0; j <=cols; j++) {
                printf("%d ", B[i][j]);
            }
            printf("\n");
        }
    }
    
    void swap_columns(int B[][COLS], int l, int r, int rows)
    {
        int tmp;
        for (int i = 0; i <= rows; i++) {
            tmp = B[i][l];
            B[i][l] = B[i][r];
            B[i][r] = tmp;
        }
    }
    
    
    void matrix_2d_rotation(int B[][COLS], int rows, int cols)
    {
        int tmp;
        // Transpose the matrix first
        for (int i = 0; i <= rows; i++) {
            for (int j = i; j <=cols; j++) {
                tmp = B[i][j];
                B[i][j] = B[j][i];
                B[j][i] = tmp;
            }
        }
        // Swap the first and last col and continue until
        // the middle.
        for (int i = 0; i < (cols / 2); i++)
            swap_columns(B, i, cols - i, rows);
    }
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int B[ROWS][COLS] = { 
                      {1, 2, 3, 4, 5}, 
                          {6, 7, 8, 9, 10},
                              {11, 12, 13, 14, 15},
                              {16, 17, 18, 19, 20},
                              {21, 22, 23, 24, 25}
                            };
    
        matrix_2d_rotation(B, ROWS - 1, COLS - 1);
    
        print_matrix_b(B, ROWS - 1, COLS -1);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题