Skip to content

Commit 708c5cb

Browse files
chore: add LeetCode daily solution
1 parent 74ea41a commit 708c5cb

5 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Rotate Image (Medium)
2+
3+
**Problem ID:** 48
4+
**Date:** 2026-05-04
5+
**Link:** https://leetcode.com/problems/rotate-image/
6+
7+
## Approach
8+
9+
To solve the problem of rotating an n x n matrix (image) by 90 degrees clockwise in-place, we can break down the solution into two main steps: transposing the matrix and then reversing the rows.
10+
11+
### Approach:
12+
13+
1. **Transpose the Matrix**:
14+
- The first step is to transpose the matrix, which means converting rows into columns. For an element at position `(i, j)`, it will move to `(j, i)`. This can be done in-place by swapping elements symmetrically across the main diagonal (from top-left to bottom-right).
15+
- Specifically, for each element in the upper triangle of the matrix (where `i < j`), swap `matrix[i][j]` with `matrix[j][i]`.
16+
17+
2. **Reverse Each Row**:
18+
- After transposing the matrix, the next step is to reverse each row. This will effectively rotate the image by 90 degrees clockwise. For each row, swap the elements from the start and end moving towards the center.
19+
- This can be accomplished by iterating through each row and swapping elements at indices `j` and `n-1-j` for `j` from `0` to `n/2`.
20+
21+
### Data Structures:
22+
- The solution operates directly on the input matrix, so no additional data structures are needed. We manipulate the matrix in-place, adhering to the problem's constraints.
23+
24+
### Complexity:
25+
- **Time Complexity**: O(n^2) - Both the transposition and the row reversal involve iterating through all elements of the n x n matrix.
26+
- **Space Complexity**: O(1) - Since we are modifying the matrix in-place, we do not use any extra space proportional to the input size.
27+
28+
This approach efficiently rotates the image while fulfilling the requirement to do so in-place, making it optimal for the problem constraints.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public void rotate(int[][] matrix) {
3+
int n = matrix.length;
4+
// First, transpose the matrix
5+
for (int i = 0; i < n; i++) {
6+
for (int j = i; j < n; j++) {
7+
int temp = matrix[i][j];
8+
matrix[i][j] = matrix[j][i];
9+
matrix[j][i] = temp;
10+
}
11+
}
12+
// Then, reverse each row
13+
for (int i = 0; i < n; i++) {
14+
for (int j = 0; j < n / 2; j++) {
15+
int temp = matrix[i][j];
16+
matrix[i][j] = matrix[i][n - 1 - j];
17+
matrix[i][n - 1 - j] = temp;
18+
}
19+
}
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var rotate = function(matrix) {
2+
const n = matrix.length;
3+
4+
// First, transpose the matrix
5+
for (let i = 0; i < n; i++) {
6+
for (let j = i + 1; j < n; j++) {
7+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
8+
}
9+
}
10+
11+
// Then, reverse each row
12+
for (let i = 0; i < n; i++) {
13+
matrix[i].reverse();
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def rotate(self, matrix: List[List[int]]) -> None:
3+
n = len(matrix)
4+
# First, transpose the matrix
5+
for i in range(n):
6+
for j in range(i, n):
7+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
8+
9+
# Then, reverse each row
10+
for i in range(n):
11+
matrix[i].reverse()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
443443
- 2026-05-01 — [Rotate Function](https://leetcode.com/problems/rotate-function/) (Medium) → `Medium/2026-05-01-396-Rotate-Function`
444444
- 2026-05-02 — [Rotated Digits](https://leetcode.com/problems/rotated-digits/) (Medium) → `Medium/2026-05-02-788-Rotated-Digits`
445445
- 2026-05-03 — [Rotate String](https://leetcode.com/problems/rotate-string/) (Easy) → `Easy/2026-05-03-796-Rotate-String`
446+
- 2026-05-04 — [Rotate Image](https://leetcode.com/problems/rotate-image/) (Medium) → `Medium/2026-05-04-48-Rotate-Image`

0 commit comments

Comments
 (0)