搜索此博客

2012年8月16日星期四

Rotate Image by 90 Degree


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?
01 public void rotate(int[][] matrix) {
02   if(matrix.length<=1)
03     return;
04   int start=0;
05   int end = matrix.length -1;
06 
07   while(start<end)
08   {
09     for(int i=0;i<end-start;i++)
10     {
11       int temp=matrix[start][start+i];
12       matrix[start][start+i]=matrix[end-i][start];
13       matrix[end-i][start]=matrix[end][end-i];
14       matrix[end][end-i]=matrix[start+i][end];
15       matrix[start+i][end]=temp;
16     }
17     start++;
18     end--;
19   }
20 
21 }

没有评论:

发表评论