103. Rotate Image

Medium · Array

You are given an n × n 2D matrix representing an image. Rotate the image 90 degrees clockwise in-place.

In-place means you must modify the matrix directly without using an extra matrix for storage. After rotation, the first row becomes the last column, the second row becomes the second-to-last column, and so on.

Examples

Example 1
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Explanation: The matrix is rotated 90 degrees clockwise. The top-left corner (1) moves to top-right, the top-right corner (3) moves to bottom-right, etc.
Example 2
Input: [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Explanation: A 2×2 matrix rotated clockwise: 1→top-right, 2→bottom-right, 3→top-left, 4→bottom-left.

Constraints