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?

思路:对角线对称呼唤,Y轴对称互换

public class Solution {
    public void rotate(int[][] matrix) {
        if(matrix==null||matrix.length==0)return;
        for(int i=0;i<=matrix.length;i++){
            for(int j=i;j<matrix[0].length;j++){
                int tmp=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=tmp;
            }
        }
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length/2;j++){
                int tmp=matrix[i][j];
                matrix[i][j]=matrix[i][matrix[0].length-1-j];
                matrix[i][matrix[0].length-1-j]=tmp;
            }
        }
        
    }
}

 

posted on 2016-10-13 10:15  Machelsky  阅读(152)  评论(0编辑  收藏  举报