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:
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]
]
Example 2:
Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
方法一:(java)
转置 + 左右对称翻转
The idea was firstly transpose the matrix and then flip it symmetrically. For instance,
1 2 3
4 5 6
7 8 9
after transpose, it will be swap(matrix[i][j], matrix[j][i])
1 4 7
2 5 8
3 6 9
Then flip the matrix horizontally. (swap(matrix[i][j], matrix[i][matrix.length-1-j])
7 4 1
8 5 2
9 6 3
public class Solution {
public void rotate(int[][] matrix) {
for(int i = 0; i<matrix.length; i++){
for(int j = i; j<matrix[0].length; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i =0 ; i<matrix.length; i++){
for(int j = 0; j<matrix.length/2; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix.length-1-j];
matrix[i][matrix.length-1-j] = temp;
}
}
}
}
改进:
把矩阵转置,矩阵左右水平对应翻转,提取出来:
public static void transposeMatrix(int[][] matrix) {
for(int i = 0; i<matrix.length; i++){
for(int j = i; j<matrix[0].length; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
public static void flipHorizontally(int[][] matrix) {
for(int i =0 ; i<matrix.length; i++){
for(int j = 0; j<matrix.length/2; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix.length-1-j];
matrix[i][matrix.length-1-j] = temp;
}
}
}
public static void rotate(int[][] matrix) {
transposeMatrix(matrix);
flipHorizontally(matrix);
}
小结
1) 一开始,已经看出了顺时针旋转90°后,矩阵元素的对应关系是:matrix[i][j] = matrix[j][length-1-j]
2)当matrix[i][j]转置以后,就变成:matrix[j][i]
3)matrix[j][i],如何变成matrix[j][length-1-j],就需要进行左右水平翻转
python:
Most Pythonic - [::-1]
and zip
- 44 ms
The most pythonic solution is a simple one-liner using [::-1]
to flip the matrix upside down and then zip
to transpose it. It assigns the result back into A
, so it's "in-place" in a sense and the OJ accepts it as such, though some people might not.
class Solution:
def rotate(self, A):
A[:] = zip(*A[::-1])
旁注:
In [1]: matrix = [[1,2,3],[4,5,6],[7,8,9]]
In [2]: matrix
Out[2]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [3]: matrix.reverse()
In [4]: matrix
Out[4]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
In [5]: matrix.reverse()
In [6]: matrix
Out[6]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [8]: matrix[:] = matrix[::-1]
In [9]: matrix
Out[9]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
matrix.reverse()和matrix[:] = matrix[::-1],作用都是上下对称翻转矩阵的;
In [12]: matrix
Out[12]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [13]: matrix[:] = zip(*matrix[::-1])
In [14]: matrix
Out[14]: [(7, 4, 1), (8, 5, 2), (9, 6, 3)] ------- 每一行都是一个tuple,而不再是一个list
In [20]: matrix = [[1,2,3],[4,5,6],[7,8,9]]
In [21]: matrix[:] = zip(*matrix[:])
In [22]: matrix
Out[22]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
matrix[:] = zip(*matrix[::-1]),zip(*)用来对矩阵进行转置
While the OJ accepts the above solution, the the result rows are actually tuples, not lists, so it's a bit dirty. To fix this, we can just apply list
to every row:
class Solution:
def rotate(self, A):
A[:] = map(list, zip(*A[::-1]))
In [23]: matrix = [[1,2,3],[4,5,6],[7,8,9]]
In [24]: matrix[:] = map(list, zip(*matrix[::-1]))
In [25]: matrix
Out[25]: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]