leetcode 67: Rotate Image

Rotate ImageMar 18 '12

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) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int n = matrix.length-1;
        if(n<1) return;
        
      //  assert n==matrix[0].length : "not a square matrix.";
        
        
        for(int i=0; i<=n/2;i++) {
            for(int j=i; j<n-i; j++) {
                int temp = 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] = temp;
            }
        }
    }
}



posted @ 2013-02-05 07:05  西施豆腐渣  阅读(136)  评论(0编辑  收藏  举报