CodeSpeek

Rotate Image

Medium · Math & Geometry

You are given a square grid of integers representing an image. Rotate the grid 90 degrees clockwise, modifying it directly without allocating a separate grid for the result. Return nothing.

Examples

Input:  matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Why:    Each layer of the square is rotated clockwise by shifting its four sides.
Input:  matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Why:    The 2x2 grid rotates so the first column becomes the first row in reverse.
Input:  matrix = [[1]]
Output: [[1]]
Why:    A single cell is unchanged by rotation.

Constraints

1 <= n <= 20, -1000 <= matrix[i][j] <= 1000, where matrix is an n x n grid

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Rotate Image

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Rotate Image. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.