LeetCode_Rotate Image

Rotate Image

You are given an nxn 2D matrix representing an image. Rotate the image by 90 degrees (clockwise).
(顺时针旋转矩阵90度)

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
14
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)

for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for j in range(n//2):
matrix[i][j], matrix[i][n-1-j] = matrix[i][n-1-j], matrix[i][j]
# matrix[i][:] = matrix[i][::-1]