Skip to content

Commit 96231bd

Browse files
committed
solve(w15): 48. Rotate Image
1 parent c8b3b43 commit 96231bd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

rotate-image/seungriyou.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://leetcode.com/problems/rotate-image/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def rotate(self, matrix: List[List[int]]) -> None:
7+
"""
8+
Do not return anything, modify matrix in-place instead.
9+
"""
10+
"""
11+
[Complexity]
12+
- TC: O(n^2) (diagonal = n * (n - 1) / 2)
13+
- SC: O(1)
14+
15+
[Approach]
16+
clockwise rotation을 in-place로 하려면 다음의 순서로 수행한다.
17+
1. matrix reverse (by row)
18+
2. matrix transpose (diagonal)
19+
"""
20+
21+
n = len(matrix)
22+
23+
# 1. matrix reverse (by row)
24+
for r in range(n // 2):
25+
matrix[r], matrix[n - r - 1] = matrix[n - r - 1], matrix[r]
26+
27+
# 2. matrix transpose (diagonal)
28+
for r in range(n):
29+
for c in range(r):
30+
matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]

0 commit comments

Comments
 (0)