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].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
This problem challenges us to rotate a square matrix (image) 90 degrees clockwise in-place, meaning without using extra space proportional to the input size. The optimal approach leverages two key steps:
1. Transpose: A matrix transpose swaps rows and columns. If we transpose the input matrix, we're halfway to a 90-degree rotation.
2. Reverse Rows: After the transpose, we need to reverse each row to complete the 90-degree clockwise rotation.
Why this works: Consider a single element at matrix[i][j]
.
matrix[j][i]
.i
moves to index n-1-i
, where n
is the dimension of the square matrix. Therefore, the final position of the element is matrix[j][n-1-i]
, which is precisely the location after a 90-degree clockwise rotation.Time Complexity: O(n²) - We iterate through the matrix twice (transpose and row reversal), each iteration taking O(n²) time.
Space Complexity: O(1) - We perform operations directly on the input matrix, using only a constant amount of extra space.
The following code snippets demonstrate the solution across various programming languages. They all follow the same two-step approach: transpose and reverse rows. Note the slight variations in how the transpose and row reversal are implemented for optimal efficiency.
Python:
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)
# Transpose
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reverse rows
for i in range(n):
matrix[i].reverse()
Java:
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// Transpose
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Reverse rows
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n - 1 - j];
matrix[i][n - 1 - j] = temp;
}
}
}
}
C++:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
// Transpose
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
// Reverse rows
for (int i = 0; i < n; i++) {
reverse(matrix[i].begin(), matrix[i].end());
}
}
};
JavaScript:
var rotate = function(matrix) {
let n = matrix.length;
// Transpose
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
}
}
// Reverse rows
for (let i = 0; i < n; i++) {
matrix[i].reverse();
}
};
These are examples; other languages would follow a similar structure. The core idea remains the same: efficiently transpose and then reverse rows to achieve the in-place 90-degree clockwise rotation.