Rotate Image
此题关键就是发现每次四个点坐标之间的联系并利用一个临时变量将它们一一迭代即可,规律不是很好描述,看代码即可
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
left, up = 0, 0
right, bottom = n - 1, n - 1
step = n - 1
while left < right and up < bottom:
for i in xrange(left, right):
tmp = matrix[up][i]
matrix[i][step - up], tmp = tmp, matrix[i][step - up]
matrix[step - up][step - i], tmp = tmp, matrix[step - up][step - i]
matrix[step - i][up], tmp = tmp, matrix[step - i][up]
matrix[up][i], tmp = tmp, matrix[up][i]
left += 1
up += 1
right -= 1
bottom -= 1
这里有个discussion对反转矩阵进行了泛化讨论,值得一看:https://discuss.leetcode.com/topic/6796/a-common-method-to-rotate-the-image/2