We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c8b3b43 commit 96231bdCopy full SHA for 96231bd
1 file changed
rotate-image/seungriyou.py
@@ -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