Rotate Image
Given an n x n matrix, return the matrix rotated 90 degrees clockwise.
Examples
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Input: [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Hints
Consider how the positions of elements change when rotating the matrix. Think about the relationship between the original and rotated positions.
Try performing the rotation in-place by carefully swapping elements in layers or cycles, without using extra space for a new matrix.
For an element at position (i, j) in the original matrix, determine its new position (i', j') in the rotated matrix by analyzing the rotation transformation.
Related Problems
Rotate Image
Given an n x n matrix, return the matrix rotated 90 degrees clockwise.