Matrix Rotation

Languages

Given an n x n two-dimensional array matrix, rotate the matrix 90 degrees clockwise in-place. This means the input two-dimensional matrix should be modified directly without using any additional 2D matrices.

Input

  • matrix: number[][]: A 2D array matrix of size n x n

Examples

Input: matrix = [[1,4],[2,6]]
Output: [[2,1],[6,4]]
Explanation: The 2x2 matrix is rotated 90 degrees clockwise.
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Explanation: The 3x3 matrix is rotated 90 degrees clockwise.
Input: matrix = [[3]]
Output: [[3]]
Explanation: The 1x1 matrix remains the same after rotation.

Constraints

  • n = matrix.length = matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[row][col] <= 1000
  • Use O(1) extra space