Transpose and Reverse

Rotate Image

January 4, 2024
medium
Transpose and Reverse

Problem: # 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 without using another 2D matrix. Example: # Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Solution: # Algorithm: Transpose and Reverse The algorithm works in two steps. First, transpose the matrix (swap rows with columns), and then reverse each row. ...