48. Rotate Image (旋转图像)
题目链接: 48. Rotate Image
Difficulty: Medium
Topics: Array, Matrix, Math
Question
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:

Input: matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
Output: [
[7, 4, 1],
[8, 5, 2],
[9, 6, 3],
]
Example 2:

Input: matrix = [
[5, 1, 9, 11],
[2, 4, 8, 10],
[13, 3, 6, 7],
[15, 14, 12, 16],
]
Output: [
[15, 13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7, 10, 11],
]
Constraints:
解题思路
方法1:
思路
题目要求顺时针转动90度,也就是把之前每一个二维数组的第0位元素按照顺序拿出来组成第0个二维数组;同理原来每一个二维数组中的第1位按照顺序拿出来组成第1个二维数组。
-
那第一步就是把整个数组翻转。
-
翻转后原来的数组就变成了如下:
[[7,8,9],
[4,5,6],
[1,2,3]] -
对比一下和我们的目标会发现位置反了(就是转置矩阵啊喂Transpose),那我们对矩阵进行转置就行了。
代码
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
# Step 1: 上下翻转
matrix[:] = matrix[::-1]
# Step 2: 转置
n = len(matrix)
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
复杂度
- 时间:因为我们对原来的矩阵进行了翻转(使用了),这部分的时间复杂度为。然后进行了两层for循环,总共遍历了次,时间消耗可以写为,总和为。
- 空间:因为都是按照题目的要求进行了in place替换,所以空间复杂度仅为。