Problem
Rotate a matrix for about 90 degree.
Algorithm
Find the mapping of the swap space in the four sides. Then loop form the outer edge to the center and swap the four sides in loops.
Code
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
mSize = len(matrix)
for i in range(mSize // 2):
for j in range(mSize - 1 - i - i):
buffer = matrix[i][i+j]
matrix[i][i+j] = matrix[mSize-1-i-j][i]
matrix[mSize-1-i-j][i] = matrix[mSize-1-i][mSize-1-i-j]
matrix[mSize-1-i][mSize-1-i-j] = matrix[i+j][mSize-1-i]
matrix[i+j][mSize-1-i] = buffer