Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Google
Meta
Rotate Image

Rotate an n×nn \times n matrix 90 degrees clockwise in place.

Acceptance 100%
Problem Statement

Problem

Given an n×nn \times n matrix of integers, rotate the matrix 90 degrees clockwise in place.

You must modify the input matrix directly and use only O(1)O(1) extra space.

Intuition

A clockwise rotation changes the position of every element according to its row and column indices. The task is to update the matrix without creating a second matrix.

Input Format

  • A square matrix matrix of size n×nn \times n.
  • matrix[i][j] is an integer value.

Output Format

  • Modify matrix in place so that it represents the same matrix rotated 90 degrees clockwise.
  • No separate return value is required beyond the mutated matrix state.

Constraints

  • 1n10001 \le n \le 1000
  • The matrix is square.
  • Use O(1)O(1) extra space.
  • The matrix values fit in standard 32-bit signed integers.
Examples
Sample cases returned by the problem API.

Example 1

Input

matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output

[[7,4,1],[8,5,2],[9,6,3]]

Explanation

After rotating 90 degrees clockwise, the first row becomes the first column from bottom to top.

Example 2

Input

matrix = [[5,1],[9,11]]

Output

[[9,5],[11,1]]

Explanation

Each element shifts to its rotated position within the 2x2 matrix.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.