Rotate Image

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?

思路:

  画画图就知道该怎么样替换了

我的代码:

public class Solution {

    public void rotate(int[][] matrix) {

        if(matrix == null || matrix.length == 0 || matrix[0].length == 0)   return;

        int n = matrix.length;

        for(int i = 0; i < n/2; i++)

        {

            for(int j = i; j < n - i - 1; j++)

            {

                int first = matrix[i][j];

                matrix[i][j] = matrix[n-j-1][i];

                matrix[n-j-1][i] = matrix[n-i-1][n-j-1];

                matrix[n-i-1][n-j-1] = matrix[j][n-i-1];

                matrix[j][n-i-1] = first;

            }

        }

        return;

    }

}
View Code

学习之处:

  swap方法的扩展,按照这样的替换方法 tmp = A ; A = B ; B = C; C = D; D=tmp;

你可能感兴趣的:(image)