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
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Explanation: Rotating clockwise turns the first column [7,4,1] (read bottom to top) into the first row. The same happens for every column, giving [[7,4,1],[8,5,2],[9,6,3]].
Example 2
Input: matrix = [[1]]
Output: [[1]]
Explanation: A 1 x 1 matrix looks the same after any rotation.
Example 3
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Explanation: Rotating the 2 x 2 matrix clockwise moves 3 to the top-left, 1 to the top-right, 4 to the bottom-left and 2 to the bottom-right.
A 90-degree clockwise rotation equals two simple in-place operations: transpose the matrix (swap across the main diagonal), then reverse each row. No extra matrix needed.
Run checks all cases above. Submit evaluates all test cases.