Skip to content

Commit 9250b9e

Browse files
committed
rotate-image solution
1 parent 1f174be commit 9250b9e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
LeetCode 48. Rotate Image
3+
https://leetcode.com/problems/rotate-image/
4+
5+
summary:
6+
n x n ํ–‰๋ ฌ์„ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ํšŒ์ „(in-place, ์ถ”๊ฐ€ ๊ณต๊ฐ„์—†์ด ํ–‰๋ ฌ ์ž์ฒด๋ฅผ ์ˆ˜์ •)
7+
"""
8+
class Solution:
9+
def rotate(self, matrix: List[List[int]]) -> None:
10+
"""
11+
Do not return anything, modify matrix in-place instead.
12+
"""
13+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2), ๊ณต๊ฐ„๋ณต์žก๋„ O(1)
14+
15+
n = len(matrix)
16+
17+
# ํ–‰๊ณผ ์—ด ๋ฐ”๊พธ๊ธฐ
18+
for i in range(n):
19+
for j in range(i+1, n):
20+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
21+
22+
# ํ–‰์„ ์ขŒ์šฐ๋ฐ˜์ „ํ•˜๊ธฐ
23+
for i in range(n):
24+
for j in range(n//2):
25+
matrix[i][j], matrix[i][n-j-1] = matrix[i][n-j-1], matrix[i][j]

0 commit comments

Comments
ย (0)