Skip to content

Commit 84f1ead

Browse files
committed
Update 48. Rotate Image.py
1 parent 40f8a11 commit 84f1ead

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

48. Rotate Image.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class Solution:
11-
def rotate(self, matrix: List[List[int]]) -> None:
11+
def rotate1(self, matrix: List[List[int]]) -> None:
1212
"""
1313
Do not return anything, modify matrix in-place instead.
1414
"""
@@ -17,12 +17,33 @@ def rotate(self, matrix: List[List[int]]) -> None:
1717
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
1818
matrix[i] = matrix[i][::-1]
1919

20+
def rotate(self, matrix: List[List[int]]) -> None:
21+
start_row, start_column = 0, 0
22+
end_row, end_column = len(matrix) - 1, len(matrix) - 1
23+
while start_row < end_row:
24+
self.rotateEdge(matrix, start_row, start_column, end_row, end_column)
25+
start_row += 1
26+
start_column += 1
27+
end_row -= 1
28+
end_column -= 1
29+
30+
def rotateEdge(self, matrix: list, start_row: int, start_column: int, end_row: int, end_column: int) -> None:
31+
times = end_row - start_row
32+
for i in range(times):
33+
temp = matrix[start_row][start_column + i]
34+
matrix[start_row][start_column + i] = matrix[end_row - i][start_column]
35+
matrix[end_row - i][start_column] = matrix[end_row][end_column - i]
36+
matrix[end_row][end_column - i] = matrix[start_row + i][end_column]
37+
matrix[start_row + i][end_column] = temp
38+
2039

2140
if __name__ == '__main__':
2241
A = [
23-
[1, 2, 3],
24-
[4, 5, 6],
25-
[7, 8, 9]
42+
[5, 1, 9, 11],
43+
[2, 4, 8, 10],
44+
[13, 3, 6, 7],
45+
[15, 14, 12, 16]
2646
]
47+
# Solution().rotate(A)
2748
Solution().rotate(A)
2849
print(A)

0 commit comments

Comments
 (0)