Use common method to rotate matrix in clockwise.
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# reverse rows
left, right = 0, len(matrix)-1
while left <= right:
matrix[left], matrix[right] = matrix[right], matrix[left]
left += 1
right -= 1
# swap the symmetry
for i in range(len(matrix)):
for j in range(i+1, len(matrix[i])):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
Rotate matrix in anticlockwise:
# reverse each row
for i in range(len(maxtrix)):
reverse(matrix[i])
# swap the symmetry
for i in range(len(matrix)):
for j in range(i+1, len(matrix[i])):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
No comments:
Post a Comment