首页 > 代码库 > Rotate Image

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?

我直接开了一个大小相同的数组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             }//for10             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