InferenceAI

Enter at least two characters to search.

Start free

Rotate a Matrix

mediumArrays & Strings25 min25 XP

Return the n×nn \times n matrix rotated 90° clockwise.

Requirements

  • In-place version: transpose, then reverse each row.

Examples

EXAMPLE WALKTHROUGH3 x 3

A guided view of the published example, not a trace of your code.

INPUT[[[1,2,3],[4,5,6],[7,8,9]]]
TASKrotate_matrix()
EXPECTED OUTPUT[[7,4,1],[8,5,2],[9,6,3]]

Start with the input values shown in this example.

Example 1 of 3

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output: [[7,4,1],[8,5,2],[9,6,3]]

Explanation: 3 x 3

Input: matrix = [[1]]

Output: [[1]]

Explanation: 1 x 1

Input: matrix = [[1,2],[3,4]]

Output: [[3,1],[4,2]]

Explanation: 2 x 2

Hint 1

Transposing swaps rows and columns; reversing each row then completes the clockwise turn.

Topics

DSAArrays & StringsPython