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:
n == matrix.length == matrix[i].length1 <= n <= 20-1000 <= matrix[i][j] <= 1000Problem Overview: You are given an n x n matrix representing an image. Rotate the matrix 90 degrees clockwise in-place, meaning you must modify the original matrix without allocating another matrix. The challenge is performing the rotation using only constant extra space.
Approach 1: Transpose and Reverse (Time: O(n^2), Space: O(1))
This approach relies on a key property of matrix rotation. A 90-degree clockwise rotation can be decomposed into two operations: transpose the matrix, then reverse every row. Transposing swaps elements across the main diagonal so that matrix[i][j] becomes matrix[j][i]. After the transpose, each row is reversed using a two-pointer swap from both ends. The transpose step touches every cell once, and the row reversal also processes each element once, resulting in O(n^2) time with O(1) extra space. This method is short, easy to reason about, and commonly expected in interviews involving matrix manipulation.
Approach 2: Layer by Layer Rotation (Time: O(n^2), Space: O(1))
This method rotates the matrix in concentric layers, starting from the outer border and moving toward the center. Each layer performs a four-way swap: top → right → bottom → left → top. For every position in the current layer, you store one value temporarily and rotate the other three into their new positions. The indices are calculated based on the current layer boundaries (first and last). Every element participates in exactly one rotation cycle, so the total work is still O(n^2). No additional data structures are required, giving O(1) space. This approach demonstrates a strong understanding of index manipulation and is often used when practicing array and matrix transformations.
Recommended for interviews: The transpose + reverse approach is usually the expected answer. It is concise, easy to implement, and clearly shows that you recognize the mathematical relationship behind a 90-degree rotation. Interviewers often appreciate candidates who first reason about the matrix transformation and then implement the two-step process. The layer-by-layer method is also valid and demonstrates deeper control over index calculations, which can be useful in problems involving geometric transformations or math-based matrix operations.
This approach utilizes two main operations. First, we transpose the matrix, which means flipping it over its diagonal. In other words, swap matrix[i][j] with matrix[j][i]. After transposing the matrix, we reverse each row. This combination results in a 90-degree clockwise rotation.
The code first transposes the given matrix in place using two nested loops. Then, for each row, it reverses the elements to achieve a 90-degree clockwise rotation.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) - Because we loop through each element once.
Space Complexity: O(1) - No extra space is used, operations are in-place.
This approach rotates the matrix layer by layer or ring by ring. Start from the outer layer and move to the inner layer, rotating elements by moving them in groups of four. This involves swapping the elements in four-step rotations.
The C solution processes the matrix in concentric layers, shifting each element appropriately to its new position during the 90-degree rotation.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) - Each individual element is moved once.
Space Complexity: O(1) - Done entirely in place, without additional memory.
| Approach | Complexity |
|---|---|
| Transpose and Reverse | Time Complexity: O(n^2) - Because we loop through each element once. |
| Layer by Layer Rotation | Time Complexity: O(n^2) - Each individual element is moved once. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Transpose and Reverse | O(n^2) | O(1) | Best general solution. Simple implementation and commonly expected in coding interviews. |
| Layer by Layer Rotation | O(n^2) | O(1) | Useful when practicing index manipulation or when explicitly rotating matrix rings. |
Rotate Matrix/Image by 90 Degrees | Brute - Optimal • take U forward • 324,741 views views
Watch 9 more video solutions →Practice Rotate Image with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor