Rotate Image ๐
Last updated
Last updated
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.
Example 1:
Example 2:
To rotate the image by 90 degrees clockwise, we can follow these steps:
Transpose the matrix (swap the elements along the main diagonal).
Reverse each row of the transposed matrix.
This process effectively rotates the matrix by 90 degrees clockwise.
Transpose the matrix by swapping the elements matrix[i][j]
and matrix[j][i]
.
Reverse each row of the transposed matrix using a two-pointer approach.
Perform these operations in-place to avoid using additional memory.
If you have any questions or need further assistance, feel free to ask! ๐ฌ I'm here to help. ๐