Rotate Image

Medium Math & Geometry AI round

matrix is a square grid of whole numbers, with n rows and n columns.

Turn it a quarter turn clockwise: the number in row i and column j moves to row j and column n - 1 - i, counting rows and columns from 0. So the first row becomes the last column, and the last row becomes the first column.

Change matrix in place, so that it holds the turned grid when your function ends. Return nothing.

Example 1

Input: matrix = [[2,7,1],[9,-4,6],[0,5,8]]
Output: [[0,9,2],[5,-4,7],[8,6,1]]

The first row, 2 7 1, becomes the last column, read from top to bottom. The last row, 0 5 8, becomes the first column. The -4 in the middle has nowhere to go and stays.

Example 2

Input: matrix = [[10,20],[30,40]]
Output: [[30,10],[40,20]]

Example 3

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

Constraints

  • 1 ≤ n ≤ 500
  • matrix has n rows, and every row has n numbers
  • -1000 ≤ matrix[i][j] ≤ 1000

Watch the explanation

More Math & Geometry

All Math & Geometry problems