Skip to content

Commit 24a2c2d

Browse files
committed
rotate-image solution (py)
1 parent 0cd8a25 commit 24a2c2d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

rotate-image/hi-rachel.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
https://leetcode.com/problems/rotate-image/description/
3+
4+
TC: O(n^2)
5+
SC: O(1)
6+
"""
7+
8+
from typing import List
9+
10+
class Solution:
11+
def rotate(self, matrix: List[List[int]]) -> None:
12+
"""
13+
Do not return anything, modify matrix in-place instead.
14+
"""
15+
top, bottom = 0, len(matrix) - 1
16+
17+
while top < bottom:
18+
left, right = top, bottom
19+
20+
for i in range(bottom - top):
21+
topLeft = matrix[top][left + i]
22+
matrix[top][left + i] = matrix[bottom - i][left]
23+
matrix[bottom - i][left] = matrix[bottom][right - i]
24+
matrix[bottom][right - i] = matrix[top + i][right]
25+
matrix[top + i][right] = topLeft
26+
27+
top, bottom = top + 1, bottom - 1
28+

0 commit comments

Comments
 (0)