首页 > 其他 > 详细

Rotate Image

时间:2014-02-08 09:22:02      阅读:336      评论:0      收藏:0      [点我收藏+]

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?

bubuko.com,布布扣
 1 public class Solution {
 2     public void rotate(int[][] matrix) {
 3         int len = matrix.length;
 4         if(len<=0) return;
 5         for(int i=0;i<len-1;i++){
 6             for(int j=0;j<len-i;j++){
 7                 swap(matrix,i,j,len-1-j,len-1-i);
 8             }
 9         }
10         for(int i=0;i<len/2;i++){
11             for(int j=0;j<len;j++){
12                 swap(matrix,i,j,len-1-i,j);
13             }
14         }
15     }
16     public void swap(int[][]matrix,int x,int y,int x2,int y2){
17         int temp = matrix[x][y];
18         matrix[x][y] = matrix[x2][y2];
19         matrix[x2][y2] = temp;
20     }
21 }
View Code

Rotate Image

原文:http://www.cnblogs.com/krunning/p/3540030.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!