-
Notifications
You must be signed in to change notification settings - Fork 0
/
#48 Rotate Image
55 lines (52 loc) · 1.62 KB
/
#48 Rotate Image
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
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix) - 1
a, b = 0, 0
count = 0
while n >= 1:
count += 1
con = []
for _ in range(n):
con.append(matrix[a][b])
b += 1
for _ in range(n):
con.append(matrix[a][b])
matrix[a][b] = con[0]
del con[0]
a += 1
for _ in range(n):
con.append(matrix[a][b])
matrix[a][b] = con[0]
del con[0]
b -= 1
for _ in range(n):
con.append(matrix[a][b])
matrix[a][b] = con[0]
del con[0]
a -= 1
for _ in range(n):
matrix[a][b] = con[0]
del con[0]
b += 1
a, b = count, count
#print(matrix, n, count)
n -= 2
## Initially transpose, (전치행렬) then flip horizontally, (discussion forum ans)
class Solution(object):
def rotate(self, matrix):
n = len(matrix)
# transpose
for i in range(n):
for j in range(i + 1, n):
tmp = matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = tmp
# flip horizontally
for i in range(n):
for j in range(n/2):
tmp = matrix[i][j]
matrix[i][j] = matrix[i][n - 1 - j]
matrix[i][n - 1- j] = tmp