-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01_rotate_image.py
59 lines (41 loc) · 1.19 KB
/
01_rotate_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution:
def rotate(self, matrix: list[list[int]]) -> None:
l, r = 0, len(matrix)- 1
while l < r:
for i in range(r - l):
top, bottom = l, r
# Save the top left
topLeft = matrix[top][l + i]
# Move bottom left into top left
matrix[top][l + i] = matrix[bottom - i][l]
# Move bottom right into bottom left
matrix[bottom - i][l] = matrix[bottom][r - i]
# Move top right into bottom right
matrix[bottom][r - i] = matrix[top + i][r]
# Move top left into top right
matrix[top + i][r] = topLeft
r -= 1
l += 1
if __name__ == "__main__":
obj = Solution()
matrix1 = [
[1,2,3],
[4,5,6],
[7,8,9]
]
obj.rotate(matrix=matrix1)
print(matrix1)
matrix2 = [
[5,1,9,11],
[2,4,8,10],
[13,3,6,7],
[15,14,12,16]
]
obj.rotate(matrix=matrix2)
print(matrix2)
matrix3 = [
[1,2],
[3,4]
]
obj.rotate(matrix=matrix3)
print(matrix3)