-
Notifications
You must be signed in to change notification settings - Fork 0
/
48_rotate_image.py
43 lines (32 loc) · 1.1 KB
/
48_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
def print_matrix(matrix):
for r in matrix:
print(r)
# O(N^2)
class Solution(object):
def rotate(self, matrix):
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
for circle in range(n//2):
r_circle = n - circle - 1
for i in range(circle, n - circle - 1):
a = matrix[circle][i]
b, matrix[i][r_circle] = matrix[i][r_circle], a
c, matrix[r_circle][n - i - 1] = matrix[r_circle][n - i - 1], b
d, matrix[n - i - 1][circle] = matrix[n - i - 1][circle], c
matrix[circle][i] = d
if __name__ == '__main__':
print('Matrix row by row:')
row = [int(v) for v in input().split(' ')]
n = len(row)
matrix = [row]
for _ in range(1, n):
row = [int(v) for v in input().split(' ')]
if len(row) > n:
raise RuntimeError('Should have been {} values'.format(n))
else:
matrix.append(row)
Solution().rotate(matrix)
print('Rotated:')
print_matrix(matrix)