PIXELBANKv9.1.0
Menu

Rotate an n x n 2D matrix 90 degrees clockwise in-place.

Input: rows of comma-separated integers. Output: rotated matrix, one row per line.

Example:

Input:
1,2,3
4,5,6
7,8,9
Output:
7 4 1
8 5 2
9 6 3
Reasoning:
  • The input is a 3x3 matrix: 1, 2, 3 4, 5, 6 7, 8, 9
  • To rotate it 90 degrees clockwise, we need to transpose the matrix and then reverse each row: the transpose is 1, 4, 7 2, 5, 8 3, 6, 9
  • Then we reverse each row to get the rotated matrix: 7, 4, 1 8, 5, 2 9, 6, 3
  • The final output is the rotated matrix, one row per line, with the elements separated by spaces instead of commas.

Constraints:

  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Rotate Image - Medium | PixelBank