Skip to content

Commit aa650bb

Browse files
committed
Feat: 48. Rotate Image
1 parent 606da9c commit aa650bb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

rotate-image/HC-kang.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/rotate-image
3+
* T.C. O(n^2)
4+
* S.C. O(1)
5+
*/
6+
function rotate(matrix: number[][]): void {
7+
const n = matrix.length;
8+
9+
// transpose
10+
for (let i = 0; i < n; i++) {
11+
for (let j = i; j < n; j++) {
12+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
13+
}
14+
}
15+
16+
// reverse
17+
for (let i = 0; i < n; i++) {
18+
for (let j = 0; j < n / 2; j++) {
19+
[matrix[i][j], matrix[i][n - j - 1]] = [matrix[i][n - j - 1], matrix[i][j]];
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)