LeetCode 题解工作台

旋转图像

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。 请不要 使用另一个矩阵来旋转图像。 示例 1: 输入: matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出: [[…

category

3

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·数学

bolt

答案摘要

根据题目要求,我们实际上需要将 旋转至 $\text{matrix}[j][n - i - 1]$。 我们可以先对矩阵进行上下翻转,即 和 $\text{matrix}[n - i - 1][j]$ 进行交换,然后再对矩阵进行主对角线翻转,即 和 进行交换。这样就能将 旋转至 $\text{matrix}[j][n - i - 1]$ 了。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·数学 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给定一个 × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

 

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]

示例 2:

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

 

提示:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

 

lightbulb

解题思路

方法一:原地翻转

根据题目要求,我们实际上需要将 matrix[i][j]\text{matrix}[i][j] 旋转至 matrix[j][ni1]\text{matrix}[j][n - i - 1]

我们可以先对矩阵进行上下翻转,即 matrix[i][j]\text{matrix}[i][j]matrix[ni1][j]\text{matrix}[n - i - 1][j] 进行交换,然后再对矩阵进行主对角线翻转,即 matrix[i][j]\text{matrix}[i][j]matrix[j][i]\text{matrix}[j][i] 进行交换。这样就能将 matrix[i][j]\text{matrix}[i][j] 旋转至 matrix[j][ni1]\text{matrix}[j][n - i - 1] 了。

时间复杂度 O(n2)O(n^2),其中 nn 是矩阵的边长。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        n = len(matrix)
        for i in range(n >> 1):
            for j in range(n):
                matrix[i][j], matrix[n - i - 1][j] = matrix[n - i - 1][j], matrix[i][j]
        for i in range(n):
            for j in range(i):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
speed

复杂度分析

指标
时间complexity is O(n^2) because each element is visited during transposition and row reversal. Space complexity is O(1) since rotation is done in-place without extra matrix allocation.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Focus on in-place array manipulation rather than creating a new matrix.

  • question_mark

    Check if candidate uses both transpose and row reversal, showing understanding of index math.

  • question_mark

    Ask for an explanation of how indices map before and after rotation to test array plus math reasoning.

warning

常见陷阱

外企场景
  • error

    Trying to rotate using an extra matrix, which violates in-place constraint.

  • error

    Reversing columns instead of rows after transposition, leading to counter-clockwise rotation.

  • error

    Incorrectly swapping elements during transposition, causing overwrites and wrong output.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Rotate matrix 180 degrees clockwise in-place.

  • arrow_right_alt

    Rotate non-square matrix with minimal extra space using similar index math.

  • arrow_right_alt

    Rotate matrix counter-clockwise using transpose and column reversal pattern.

help

常见问题

外企场景

旋转图像题解:数组·数学 | LeetCode #48 中等