码迷,mamicode.com
首页 > 其他好文 > 详细

Rotate Image

时间:2015-01-11 14:50:19      阅读:154      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!