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:找规律 int[i][]--int[][n-1-i]

public class Solution {
    public void rotate(int[][] matrix) {
      int n=matrix.length;
      int[][] temp=new int[n][n];
      for(int i=0;i<n;i++){
          for(int j=0;j<n;j++){          
            temp[j][n-1-i]= matrix[i][j];         
          }
      }
      for(int i=0;i<n;i++){
          for(int j=0;j<n;j++){          
            matrix[i][j]= temp[i][j];         
          }
      }
    }
}

 

posted @ 2015-04-11 05:27  微微程序媛  阅读(106)  评论(0编辑  收藏  举报