All problems

Rotate Image

Medium · matrix

You are given a square grid of integers matrix with n rows and n columns. Turn the grid a quarter turn clockwise, as if you were rotating a photograph on a table. The first column, read from the bottom to the top, becomes the first row; the second column becomes the second row, and so on.

Return the rotated grid. It is again an n by n grid.

Examples

Example 1

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

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

The first column 1, 4, 7 read from the bottom is 7, 4, 1, which is the new first row.

Example 2

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

Output: [[3, 1], [4, 2]]

Example 3

Input: matrix = [[5]]

Output: [[5]]

A single cell looks the same after any turn.

Constraints