-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice_48.py
80 lines (67 loc) · 2.03 KB
/
practice_48.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Solution(object):
def computeNextIndex(self, i, j, start, end):
if i == start:
if j == end:
inext = i+1
jnext = j
else:
inext = i
jnext = j+1
elif i == end:
if j == start:
inext = i-1
jnext = j
else:
inext = i
jnext = j-1
else:
if j == start:
inext = i-1
jnext = j
else: # j == end
inext = i+1
jnext = j
return inext, jnext
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
# rounds = (n+1) // 2
sn = 0
en = n-1
while sn < en:
rot_counter = en-sn
while rot_counter:
i, j = sn, sn
store = matrix[i][j]
n_items = (en-sn+1)*2 + (en-sn-1)*2
while n_items:
ii, jj = self.computeNextIndex(i, j, sn, en)
# print('current', i, j)
# print('next', ii, jj)
tmp = matrix[ii][jj]
matrix[ii][jj] = store
store = tmp
i, j = ii, jj
n_items -= 1
# for line in matrix:
# print(line)
# print()
rot_counter -= 1
sn = sn+1
en = en-1
return matrix
if __name__ == '__main__':
sol = Solution()
input_matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
# for line in input_matrix:
# print(line)
# print()
sol.rotate(input_matrix)
for line in input_matrix:
print(line)
print()
for line in [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]:
print(line)