LintCode Q161 Rotate Image in Python

  • Jinhai ZHOU
  • 4 Minutes
  • 2016年11月13日
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
class Solution:
"""
@param matrix: A list of lists of integers
@return: Nothing
"""
def rotate(self, matrix):
# write your code here
# time complexity O(N^2), space complexity O(1)
# Total Runtime: 361 ms
# 100% test cases passed.
end = len(matrix) - 1
self.rotateRe(matrix, 0, end)
def rotateRe(self, matrix, start, end):
length = end - start + 1
end_matrix = len(matrix) - 1
if length < 2:
return
for x in xrange(start, end):
# save top
offset = len(matrix) - 1
top = matrix[start][x]
# left to top
matrix[start][x] = matrix[offset - x][start]
# bottom to left
matrix[offset - x][start] = matrix[end][offset - x]
# right to bottom
matrix[end][offset - x] = matrix[x][end]
# top to right
matrix[x][end] = top
self.rotateRe(matrix, start + 1, end - 1)
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。