221. Rotate Image

Medium
Transpose and Reverse
Array

Problem

Given an n x n matrix, rotate the matrix 90 degrees clockwise and return the rotated matrix.

Examples

Example 1

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

Example 2

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

Example 3

Input: [[1]]
Output: [[1]]
Constraints

1 <= matrix.length <= 100

matrix.length == matrix[i].length

Hints

?? Transpose the matrix.

?? Then reverse each row.

Expected Complexity
Time: O(n^2)
Space: O(1)
Follow-up

Can you rotate the matrix in-place?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[[1,2,3],[4,5,6],[7,8,9]]
Expected
[[7,4,1],[8,5,2],[9,6,3]]