Rotate Matrix - aloalgo

Rotate Matrix

Medium

You are given an n x n 2D matrix, which represents an image.

Your task is to rotate this image by 90 degrees clockwise.

Note that:

  • The rotation must be performed in-place, meaning you must modify the input 2D matrix directly without allocating another 2D matrix.

Do not return anything, instead modify the input matrix

Example 1

Input
[
  [1]
]
Output
[
  [1]
]
Explanation:
  1. The original matrix is:
1
  1. After rotating 90 degrees clockwise, it remains the same:
1

Example 2

Input
[
  [1, 2],
  [3, 4]
]
Output
[
  [3, 1],
  [4, 2]
]
Explanation:
  1. The original matrix is:
1 2
3 4
  1. After rotating 90 degrees clockwise, it becomes:
3 1
4 2

Example 3

Input
[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
Output
[
  [7, 4, 1],
  [8, 5, 2],
  [9, 6, 3]
]
Explanation:
  1. The original matrix is:
1 2 3
4 5 6
7 8 9
  1. After rotating 90 degrees clockwise, it becomes:
7 4 1
8 5 2
9 6 3
Loading...
Input
[
  [1]
]
Output
[
  [1]
]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!