Need to rotate an n x n
matrix by 90 degrees clockwise without using extra space? LeetCode Problem 48: Rotate Image is a popular interview question that challenges your understanding of in-place matrix manipulation.
In this post, you’ll learn how to rotate a matrix efficiently using the transpose + reverse method in Java. We’ll walk through each step, explain the logic behind the code, and provide a full complexity breakdown.
🔍 Problem Overview
Given an n x n
matrix (2D array), rotate it by 90 degrees clockwise in-place. That means you should modify the input matrix directly, without allocating a new one.
Example Input:
[[1,2,3],
[4,5,6],
[7,8,9]]
Output (rotated 90° clockwise):
[[7,4,1],
[8,5,2],
[9,6,3]]
💡 Solution Strategy: Transpose + Reverse
To rotate a matrix in-place, we use two simple but powerful steps:
- Transpose the matrix – Convert rows to columns by swapping across the diagonal.
- Reverse each row – Flip each row to achieve the final 90° clockwise rotation.
Why This Works
- Transpose changes the orientation of elements across the main diagonal.
- Reversing each row completes the clockwise transformation.
This approach is intuitive, clean, and avoids complex index math seen in layer-by-layer rotation.
✅ Java Code Solution
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// Step 1: Transpose the matrix
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Step 2: Reverse each row
for (int i = 0; i < n; i++) {
int left = 0, right = n - 1;
while (left < right) {
int temp = matrix[i][left];
matrix[i][left] = matrix[i][right];
matrix[i][right] = temp;
left++;
right--;
}
}
}
}
🔎 Step-by-Step Explanation
🔁 Step 1: Transpose the Matrix
- Swap
matrix[i][j]
withmatrix[j][i]
for alli < j
. - Converts rows into columns.
- After transpose:
[[1,4,7], [2,5,8], [3,6,9]]
↔️ Step 2: Reverse Each Row
- Use two pointers (
left
andright
) to reverse each row in-place. - Reversing each row of the transposed matrix results in:
[[7,4,1], [8,5,2], [9,6,3]]
📈 Time and Space Complexity
- Time Complexity: O(n²) – Both transposing and reversing involve nested loops over the matrix.
- Space Complexity: O(1) – All operations are performed in-place using constant extra space.
🧠 Key Insights
- Efficient In-Place Solution: No need for extra memory, just smart swaps.
- Simplified Logic: Transpose + reverse is easier to implement and debug than rotating layer by layer.
- Versatile Technique: This pattern is useful for related problems like spiral order traversal or image processing tasks.
❓ FAQs
Q1: Why use transpose and reverse instead of rotating layer by layer?
It simplifies logic and minimizes indexing errors. It’s easier to understand and maintain.
Q2: Does this work for non-square matrices?
No. This algorithm only works for square (n x n) matrices. Rotating non-square matrices requires a different approach.
Q3: How do I rotate the matrix counterclockwise?
Reverse each row before transposing the matrix for a 90° counterclockwise rotation.
Q4: Can I combine both steps into one?
Yes, but separating them makes the code clearer and easier to debug.
🧪 Related Practice Problems
These problems build upon matrix manipulation techniques and reinforce in-place operations.
🌍 Real-World Applications
Matrix rotation techniques are widely used in:
- Image Processing – Rotating images and photos.
- Game Development – Tiling systems, rotating game boards or 2D sprites.
- Computer Graphics – Transforming object perspectives in 2D and 3D environments.
🚀 Final Thoughts
Mastering in-place matrix rotation not only helps with technical interviews but also builds a strong foundation for tackling complex 2D array problems. The transpose + reverse approach is clean, elegant, and effective.
Try it out, explore edge cases (e.g., 1×1 and 2×2 matrices), and reinforce the logic with similar problems!
Let me know if you want a visual explanation or animated walkthrough!