1.代码
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+1)//2):
temp = matrix[i][j]
for count in range(4):
q = i
i = j
j = n-1-q
temp1 = matrix[i][j]
matrix[i][j] = temp
temp = temp1
2.思路
为了实现原地旋转,每一次覆盖某个位置的值之前要记录其原有值。要实现对整个矩阵的翻转,只要对i in range(n//2)和j in range((n+1)//2)的部分操作,使其对应的4块区域均旋转到所需的位置。