Skip to content

Commit

Permalink
add solution: rotate image
Browse files Browse the repository at this point in the history
  • Loading branch information
obzva committed Nov 20, 2024
1 parent 575b17e commit 8112a9c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions rotate-image/flynn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
풀이
- matrixλ₯Ό 4μ‚¬λΆ„λ©΄μœΌλ‘œ λ‚˜λˆ•λ‹ˆλ‹€
1μ‚¬λΆ„λ©΄μ˜ λͺ¨λ“  μ’Œν‘œμ— λŒ€ν•΄ μ•„λž˜μ™€ 같은 연산을 μˆ˜ν–‰ν•©λ‹ˆλ‹€
- 1μ‚¬λΆ„λ©΄μ˜ μ’Œν‘œ a1에 λŒ€ν•΄ a2, a3, a4λ₯Ό μ•„λž˜μ²˜λŸΌ μ •μ˜ν•©λ‹ˆλ‹€
a2: a1을 90도 νšŒμ „μ‹œμΌ°μ„ λ•Œμ˜ μ’Œν‘œ (2사뢄면에 μœ„μΉ˜ν•¨)
a3: a2λ₯Ό 90도 νšŒμ „μ‹œμΌ°μ„ λ•Œμ˜ μ’Œν‘œ (3사뢄면에 μœ„μΉ˜ν•¨)
a4: a3을 90도 νšŒμ „μ‹œμΌ°μ„ λ•Œμ˜ μ’Œν‘œ (4사뢄면에 μœ„μΉ˜ν•¨)
a1 -> a2, a2 -> a3, a3 -> a4, a4 -> a1으둜 값을 λ³€κ²½μ‹œν‚΅λ‹ˆλ‹€
Big O
- N: 맀트릭슀의 크기
- Time complexity: O(N^2)
- Space complexity: O(1)
*/

func rotate(matrix [][]int) {
n := len(matrix)
// μ‚¬λΆ„λ©΄μ˜ 크기, qr, qc: μ‚¬λΆ„λ©΄μ˜ ν–‰, μ—΄ 크기
qr := n / 2
qc := (n + 1) / 2

for r := 0; r < qr; r++ {
for c := 0; c < qc; c++ {
r1 := r
c1 := c

r2 := c
c2 := n - 1 - r

r3 := n - 1 - r
c3 := n - 1 - c

r4 := n - 1 - c
c4 := r

matrix[r1][c1], matrix[r2][c2], matrix[r3][c3], matrix[r4][c4] = matrix[r4][c4], matrix[r1][c1], matrix[r2][c2], matrix[r3][c3]
}
}
}

0 comments on commit 8112a9c

Please sign in to comment.