Rotate Matrix 90 Degrees

Medium~20 min

Given an n x n square matrix of integers, rotate it 90 degrees clockwise and return the rotated matrix.

For example, the top row of the original becomes the right column of the rotated matrix.

Examples

Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Explanation: Row [1,2,3] becomes the rightmost column. Row [7,8,9] becomes the leftmost column.
Example 2
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Explanation: The 2x2 matrix is rotated 90 degrees clockwise.
Example 3
Input: matrix = [[1]]
Output: [[1]]
Explanation: A 1x1 matrix is unchanged by rotation.

Constraints

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 100
  • -1000 <= matrix[i][j] <= 1000
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run