48. 旋转图像

https://leetcode-cn.com/problems/rotate-image/

解法一:坐标变换

观察得知右旋90度可以通过先转置,再左右镜像变换即可。 转置坐标变换为:(i,j) →(j,i) 左右镜像:行不变,第j列与第n-1-j列对换。

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):
            for j in range(i, n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        for j in range(n//2):
            for i in range(n):
                matrix[i][j], matrix[i][n-1-j] = matrix[i][n-1-j], matrix[i][j]

最后更新于