旋转图像-数组48-python
程序员文章站
2022-07-12 08:47:15
...
python
将原矩阵先水平翻转,再沿主对角线(左上到右下)翻转,就是顺时针翻转90度的效果了。
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
# 水平线翻转
for i in range(n//2):
for j in range(n):
matrix[i][j], matrix[n-i-1][j] = matrix[n-i-1][j], matrix[i][j]
# 对角线翻转
for i in range(n):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]