给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
方案一:先按X轴对称旋转, 再用zip()解压,最后用list重组。
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
matrix[:] = map(list, zip(*matrix[: : -1]))
方案二:找到规律,用原矩阵数据 赋值
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
m = matrix.copy()
n = len(matrix)
for i in range(n):
matrix[i] = [m[j][i] for j in range(n - 1, -1, -1)]
return