You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13
Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ],
rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ]
classSolution{ publicvoidrotate(int[][] matrix){ int n=matrix.length; helper(matrix,n,0); } publicvoidhelper(int[][] matrix, int len, int level){ if(level>=len/2) return; for(int i=level;i<len-level-1;i++){ int curr=matrix[level][i]; int nextRow=i, nextCol=len-level-1; for(int j=0;j<4;j++){ int next = matrix[nextRow][nextCol]; matrix[nextRow][nextCol]=curr; curr=next; int tmp=nextRow; nextRow=nextCol; nextCol=len-tmp-1; } } helper(matrix,len,level+1); } }
时间复杂度: O()。
空间复杂度: O()。
Solution 2
不用递归,直接在一个方法中完成,i为行数,j为列数。
1 2 3 4 5
1 2 3 7 2 1 7 4 1
4 5 6 --> 4 5 6 --> 8 5 2
7 8 9 9 8 3 9 6 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution{ publicvoidrotate(int[][] matrix){ int n=matrix.length; for(int i=0;i<n/2;i++){ for(int j=i;j<n-i-1;j++){ int tmp=matrix[i][j]; matrix[i][j]=matrix[n-j-1][i]; matrix[n-j-1][i]=matrix[n-i-1][n-j-1]; matrix[n-i-1][n-j-1]=matrix[j][n-i-1]; matrix[j][n-i-1]=tmp; } } } }
时间复杂度: O()。
空间复杂度: O()。
Solution 3
先将矩阵以对角线为轴翻转,在以x轴中线上下翻转即可。
1 2 3 4 5
1 2 3 9 6 3 7 4 1
4 5 6 --> 8 5 2 --> 8 5 2
7 8 9 7 4 1 9 6 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution{ publicvoidrotate(int[][] matrix){ int n=matrix.length; for(int i=0;i<n-1;i++){ for(int j=0;j<n-i;j++){ int tmp=matrix[i][j]; matrix[i][j]=matrix[n-j-1][n-i-1]; matrix[n-j-1][n-i-1]=tmp; } } for(int i=0;i<n/2;i++){ for(int j=0;j<n;j++){ int tmp = matrix[i][j]; matrix[i][j]=matrix[n-i-1][j]; matrix[n-i-1][j]=tmp; } } } }
时间复杂度: O()。
空间复杂度: O()。
Solution 4
先将矩阵转置,再对每一行进行翻转。
1 2 3 4 5
1 2 3 1 4 7 7 4 1
4 5 6 --> 2 5 8 --> 8 5 2
7 8 9 3 6 9 9 6 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution{ publicvoidrotate(int[][] matrix){ int n=matrix.length; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ int tmp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=tmp; } } for(int i=0;i<n;i++){ for(int j=0;j<n/2;j++){ int tmp = matrix[i][j]; matrix[i][j]=matrix[i][n-j-1]; matrix[i][n-j-1]=tmp; } } } }