Rotate Image in Python and Java
In this tutorial, we will learn LeetCode #48: Rotate Image in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the Rotate Image Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use transpose and reverse because it gives a clean and optimized solution.
Example input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Expected result: Output: [[7,4,1],[8,5,2],[9,6,3]] Explanation: Rotate 90 degrees clockwise.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: matrix = [[1,2,3],[4,5,6],[7,8,9]] Approach: transpose and reverse We update variables step by step until we reach: Output: [[7,4,1],[8,5,2],[9,6,3]]
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why transpose and reverse is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1 Use this custom example. matrix = [[1,2,3],[4,5,6],[7,8,9]] We will solve it using transpose and reverse. Step 2 Look at the first important value from the example and create the variables needed by the algorithm. current_state = based on the first value answer = not finished yet Step 3 Move to the next useful value and update the state. The algorithm compares the new value with the old state. If the new value improves the answer, we update the answer. Step 4 Continue this process until all useful values are processed. After processing the example, we get: Output: [[7,4,1],[8,5,2],[9,6,3]] Why? Rotate 90 degrees clockwise.
Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: Output: [[7,4,1],[8,5,2],[9,6,3]] Explanation: Rotate 90 degrees clockwise.
Example 2
Input: matrix = [[5,1,9],[2,4,8],[3,6,7]] Output: [[3,2,5],[6,4,1],[7,8,9]] Explanation: The image is rotated 90 degrees clockwise.
Python Code
Here is the complete Python solution for LeetCode #48.
class MatrixRotator:
def rotate(self, matrix):
n = len(matrix)
for row in range(n):
for col in range(row + 1, n):
matrix[row][col], matrix[col][row] = matrix[col][row], matrix[row][col]
for row in matrix:
row.reverse()
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
rotator = MatrixRotator()
rotator.rotate(matrix)
print(matrix) # Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]Java Code
Here is the complete Java solution for LeetCode #48.
import java.util.Arrays;
class MatrixRotator {
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int row = 0; row < n; row++) {
for (int col = row + 1; col < n; col++) {
int temp = matrix[row][col];
matrix[row][col] = matrix[col][row];
matrix[col][row] = temp;
}
}
for (int row = 0; row < n; row++) {
int left = 0;
int right = n - 1;
while (left < right) {
int temp = matrix[row][left];
matrix[row][left] = matrix[row][right];
matrix[row][right] = temp;
left++;
right--;
}
}
}
public static void main(String[] args) {
int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
MatrixRotator rotator = new MatrixRotator();
rotator.rotate(matrix);
System.out.println(Arrays.deepToString(matrix)); // [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
}
}Time and Space Complexity
Time Complexity: O(n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(n)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #48: Rotate Image becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.