首页 > 其他 > 详细

Rotate Image

时间:2015-01-11 14:48:09      阅读:234      评论: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?

我直接开了一个大小相同的数组result,然后将result中的内容copy到原数组中

 1 public class Solution {
 2         public void rotate(int[][] matrix) {
 3             int lengthOfMatrix = matrix.length;
 4             int [][]result = new int[lengthOfMatrix][lengthOfMatrix];
 5             for(int i = 0; i < lengthOfMatrix; i++){
 6                 for(int j = lengthOfMatrix - 1, k = 0; j >= 0; j--, k++){
 7                     result[i][k] = matrix[j][i];
 8                 }//for
 9             }//for
10             for(int i = 0; i < lengthOfMatrix; i++){
11                 for(int j = 0; j < lengthOfMatrix; j++){
12                     matrix[i][j] = result[i][j];
13                 }
14             }
15         }
16     }

不过题目要求在原来数组的基础上面修改,这个还要看看别人的,我这样空间复杂度就为O(N)了

 

Rotate Image

原文:http://www.cnblogs.com/luckygxf/p/4216450.html

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