forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_image.py
More file actions
32 lines (31 loc) · 950 Bytes
/
rotate_image.py
File metadata and controls
32 lines (31 loc) · 950 Bytes
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
# -*- coding: utf-8 -*-
class Solution:
"""
@param matrix: A list of lists of integers
@return: Nothing
"""
def rotate(self, matrix):
# write your code here
rows = len(matrix)
'''
原始矩阵和目标矩阵为
1 2 3 7 4 1
4 5 6 => 8 5 2
7 8 9 9 6 3
先对非对角线元素做行列转置,实际只要做上半三角。
1 4 7
2 5 8
3 6 9
然后左右往当中列互换即可。
'''
if rows > 1:
for i in xrange(rows):
for j in xrange(i + 1, rows):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
i, j = 0, rows - 1
while i < j:
for k in xrange(rows):
matrix[k][i], matrix[k][j] = matrix[k][j], matrix[k][i]
i += 1
j -= 1
return matrix