Medium · Matrices

Rotate matrix by 90 degrees

Given an n × n integer matrix mat, rotate it 90 degrees clockwise in place; the modified mat is the answer.

Examples

Example 1

{
  "mat": [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
  ]
}

Output: [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]

Example 2

{
  "mat": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
}

Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Matrices problems