题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
class Solution:
def rotateMatrix(self , mat: List[List[int]], n: int) -> List[List[int]]:
for i in range(n):
for j in range(i):
mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
for i in range(n):
mat[i].reverse()
return mat
解题思路
找规律可以发现,将矩阵顺时针旋转九十度等于先将矩阵转置,再将每一行翻转。
复杂度
时间复杂度为O(N^2);
空间复杂度为O(1)。

