Skip to content

Commit 3fcbbe1

Browse files
Jeehay28Jeehay28
authored andcommitted
Add rotate-image solution in TS
1 parent d3c1f99 commit 3fcbbe1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

rotate-image/Jeehay28.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
Do not return anything, modify matrix in-place instead.
3+
*/
4+
5+
// TC: O(n^2)
6+
// SC: O(1)
7+
function rotate(matrix: number[][]): void {
8+
let top = 0;
9+
let bottom = matrix.length - 1;
10+
11+
while (top < bottom) {
12+
let left = top;
13+
let right = bottom;
14+
15+
for (let i = 0; i < bottom - top; i++) {
16+
const temp = matrix[top][left + i]; // topLeft
17+
matrix[top][left + i] = matrix[bottom - i][left];
18+
matrix[bottom - i][left] = matrix[bottom][right - i];
19+
matrix[bottom][right - i] = matrix[top + i][right];
20+
matrix[top + i][right] = temp;
21+
}
22+
23+
top++; // top down
24+
bottom--; // bottom up
25+
}
26+
}

0 commit comments

Comments
 (0)