leetcode_Rotate Image

描述:

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

思路:

对于这种题目,就应该在纸上画画写写,由简单到一般,一般是可以发现规律的。当然了,只是有规律,newArr[j][rows-i-1]=matrix[i][j];

代码:

public void rotate(int[][] matrix) {
        int rows=matrix.length;
        int colums=matrix[0].length;
       int newArr[][]=new int[rows][colums];
       for(int i=0;i<rows;i++)
       {
           for(int j=0;j<colums;j++)
                newArr[j][rows-i-1]=matrix[i][j];
       }
       for(int i=0;i<rows;i++)
       {
           for(int j=0;j<colums;j++)
                matrix[i][j]=newArr[i][j];
       }
       
    }


你可能感兴趣的:(image,Matrix,rotate,2d)