[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?

 

Solution:

public class Solution {
    public void rotate(int[][] matrix) {
        int N=matrix.length-1;
        if(N<1)
            return;
        for(int i=0;i<=N/2;++i){
            for(int j=i;j<N-i;++j){
                int temp=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]=matrix[i][j];
                matrix[i][j]=temp;
            }
        }
    }
}

 

posted @ 2014-11-14 16:48  Phoebe815  阅读(130)  评论(0编辑  收藏  举报