Rotate a Matrix by 90 Degrees
Problem statement
Given an n × n integer matrix, rotate it 90 degrees clockwise in place and return the modified matrix.
The original value at row r and column c moves to row c and column n - 1 - r. Rows and columns use zero-based indices.
Keep the matrix square and preserve every value. An approach using temporary storage is valid; aim to reduce auxiliary space to O(1).
Function
rotateMatrix(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[1,2],[3,4]]return = [[3,1],[4,2]]The original bottom-left value 3 moves to the top-left, and the original top-right value 2 moves to the bottom-right.
Example 2
matrix = [[2,5,8],[1,4,7],[0,3,6]]return = [[0,1,2],[3,4,5],[6,7,8]]Each original column becomes a row, read from bottom to top.
Constraints
1 <= n <= 40.- Every row contains exactly
nintegers. - Every value is in
[-10^6, 10^6].