Skip to main content
CodeOath
← All problems

Problem

Rotate Image

Medium
  • matrix

Rotate the grid matrix a quarter turn clockwise and return the result. The first row becomes the last column, the second row becomes the column before it, and so on.

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

the first row [3, 8, 1] is now the last column, read from top to bottom. The center value 2 stays where it is.

Example 2
Input
matrix = [[5, 6], [7, 8]]
Output
[[7, 5], [8, 6]]
Explanation

with an even size there is no center, so every value moves. 5 goes from the top-left corner to the top-right corner.

Constraints:

  • matrix is square: it has n rows and each row has n values

Follow-up: can you rotate the values inside matrix itself, with only a few extra variables, and return it?

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.