Practice · Math and matrices · 2 of 3

Rotate a Square Grid

You are given matrix, an n × n grid of integers.

Rotate it 90 degrees clockwise in place: change matrix itself rather than building and returning a new grid. After the turn, the first row holds what was the first column, read from bottom to top.

Example 1

Input
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Example 2

Input
matrix = [[1, 2], [3, 4]]
Output
[[3, 1], [4, 2]]

Constraints

  • 1 ≤ n ≤ 20
  • Every value is between −1000 and 1000.