Skip to content

Commit b0bdb13

Browse files
committed
add: solve #281 Rotate Image with ts
1 parent 23cc4c3 commit b0bdb13

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

β€Žrotate-image/Yjason-K.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* μ£Όμ–΄μ§„ μ •μ‚¬κ°ν˜• 행렬을 90도 νšŒμ „μ‹œν‚€λŠ” ν•¨μˆ˜
3+
*
4+
* @param {number[][]} matrix - 2차원 λ°°μ—΄λ‘œ ν‘œν˜„λœ μ •μ‚¬κ°ν˜• ν–‰λ ¬.
5+
*
6+
* μ‹œκ°„ λ³΅μž‘λ„: O(n^2)
7+
* - n * n ν–‰λ ¬, λͺ¨λ“  μš”μ†Œλ₯Ό ν•œ λ²ˆμ”© λ°©λ¬Έ.
8+
*
9+
* 곡간 λ³΅μž‘λ„: O(1)
10+
* - 좔가적인 곡간 μ‚¬μš© X
11+
*/
12+
function rotate(matrix: number[][]): void {
13+
// ν–‰λ ¬μ˜ 크기 n (μ •μ‚¬κ°ν˜• ν–‰λ ¬μ΄λ―€λ‘œ ν–‰κ³Ό μ—΄μ˜ μˆ˜λŠ” 동일)
14+
const n = matrix.length;
15+
16+
// ν–‰λ ¬μ˜ λŒ€κ°μ„ μ„ κΈ°μ€€μœΌλ‘œ μ’Œν‘œ (i, j)와 (j, i)의 μš”μ†Œλ₯Ό κ΅ν™˜.
17+
for (let i = 0; i < n; i++) {
18+
// jλŠ” iλΆ€ν„° μ‹œμž‘ν•˜μ—¬ 쀑볡 κ΅ν™˜μ„ λ°©μ§€
19+
for (let j = i; j < n; j++) {
20+
// λ°°μ—΄ ꡬ쑰 λΆ„ν•΄ 할당을 μ΄μš©ν•˜μ—¬ 두 μš”μ†Œλ₯Ό μŠ€μ™‘
21+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
22+
}
23+
// 각 행을 λ°˜μ „
24+
matrix[i].reverse();
25+
}
26+
}

0 commit comments

Comments
Β (0)