Skip to content
BASA DSA

Rotate Image

MediumMath & Geometry

Rotate a square matrix clockwise by 90 degrees in place. Return nothing; use constant auxiliary space.

Example 1

0,01
0,12
1,03
1,14
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[1,2],[3,4]]
Output
[[3,1],[4,2]]

Explanation

Each (r,c) moves to (c,1-r), producing rows [3,1],[4,2].

Example 2

0,01
0,12
0,23
1,04
1,15
1,26
2,07
2,18
2,29
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]

Explanation

Column 1,4,7 becomes row 7,4,1; the other columns rotate similarly.

Example 3

0,05
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[5]]
Output
[[5]]

Explanation

A 1-by-1 matrix stays unchanged.

Constraints

  • 1 <= side length <= 20
  • Entries are signed integers

Function contractrotateArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…