Get idea from others
–
Question
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Analysis
There are two methods
1. calculate the corresponding indexes
2. reverse left to right , then swap the symmetry.
The second way is tricky.
Solution
class Solution:
# @param {integer[][]} matrix
# @return {void} Do not return anything, modify matrix in-place instead.
def rotate(self, matrix):
if len(matrix)==0 or len(matrix[0])==0:
return None
n = len(matrix)
for col in range(n/2):
for row in range(n):
matrix[row][col],matrix[row][n-1-col] = matrix[row][n-1-col], matrix[row][col]
for row in range(n):
for col in range(n-row):
matrix[row][col],matrix[n-1-col][n-1-row] = matrix[n-1-col][n-1-row],matrix[row][col]