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) {
        int n = matrix.length;
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tmp;
            }
        }
        for(int i=0; i<n; i++){
            int l=0, r=n-1;
            while(l<r){
                int tmp = matrix[i][l];
                matrix[i][l++] = matrix[i][r];
                matrix[i][r--] = tmp;
            }
        }
    }
}
comments powered by Disqus