- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/rotate-image
- topics: 2D Array
You are given an n x n
2D matrix
representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Idea:
for every row pop n last elements and insert them to the first index of corresponding row
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
for i in range(len(matrix)):
for j in range(i, len(matrix) + i):
matrix[len(matrix) - j + i - 1].insert(0, matrix[i].pop())