Skip to content

Commit 8112a9c

Browse files
committed
add solution: rotate image
1 parent 575b17e commit 8112a9c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

rotate-image/flynn.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
풀이
3+
- matrix를 4사분면으로 나눕니다
4+
1사분면의 모든 좌표에 대해 아래와 같은 연산을 수행합니다
5+
- 1사분면의 좌표 a1에 대해 a2, a3, a4를 아래처럼 정의합니다
6+
a2: a1을 90도 회전시켰을 때의 좌표 (2사분면에 위치함)
7+
a3: a2를 90도 회전시켰을 때의 좌표 (3사분면에 위치함)
8+
a4: a3을 90도 회전시켰을 때의 좌표 (4사분면에 위치함)
9+
a1 -> a2, a2 -> a3, a3 -> a4, a4 -> a1으로 값을 변경시킵니다
10+
Big O
11+
- N: 매트릭스의 크기
12+
- Time complexity: O(N^2)
13+
- Space complexity: O(1)
14+
*/
15+
16+
func rotate(matrix [][]int) {
17+
n := len(matrix)
18+
// 사분면의 크기, qr, qc: 사분면의 행, 열 크기
19+
qr := n / 2
20+
qc := (n + 1) / 2
21+
22+
for r := 0; r < qr; r++ {
23+
for c := 0; c < qc; c++ {
24+
r1 := r
25+
c1 := c
26+
27+
r2 := c
28+
c2 := n - 1 - r
29+
30+
r3 := n - 1 - r
31+
c3 := n - 1 - c
32+
33+
r4 := n - 1 - c
34+
c4 := r
35+
36+
matrix[r1][c1], matrix[r2][c2], matrix[r3][c3], matrix[r4][c4] = matrix[r4][c4], matrix[r1][c1], matrix[r2][c2], matrix[r3][c3]
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)