类型:数组
-
- 旋转图像 💛
https://leetcode-cn.com/problems/rotate-image/
❓ 把 n x n 的二维矩阵 matrix 顺时针旋转 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(i, n - i - 1): temp = matrix[i][j] for _ in range(4): matrix[j][n - 1 - i] , temp = temp, matrix[j][n - 1 - i] i, j = j, n - 1 -i时间复杂度:O(n^2),空间复杂度:O(1)