Skip to content

Commit 3dc182a

Browse files
committed
rotate image solution
1 parent fe5e3ab commit 3dc182a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

rotate-image/hyer0705.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
Do not return anything, modify matrix in-place instead.
3+
*/
4+
// Time Complexity: O(n^2), n: matrix의 한 변의 길이
5+
// Space Complexity: O(1)
6+
function rotate(matrix: number[][]): void {
7+
const n = matrix.length;
8+
9+
for (let i = 0; i < n; i++) {
10+
for (let j = i + 1; j < n; j++) {
11+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
12+
}
13+
}
14+
15+
for (let i = 0; i < n; i++) {
16+
for (let j = 0; j < Math.floor(n / 2); j++) {
17+
[matrix[i][j], matrix[i][n - j - 1]] = [matrix[i][n - j - 1], matrix[i][j]];
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)