47. 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) return;
        if(matrix.length != matrix[0].length) return;
        
        int N = matrix.length - 1;
        for(int i=0; i<(N+1)/2; i++){
            for(int j=i; j<N-i; j++){
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[N-j][i];
                matrix[N-j][i] = matrix[N-i][N-j];
                matrix[N-i][N-j] = matrix[j][N-i];
                matrix[j][N-i] = tmp;
                
            }
        }
        
    }
}

 

posted @ 2013-09-07 04:41  LEDYC  阅读(147)  评论(0)    收藏  举报