/48. Rotate Image

48. Rotate Image

Medium
Arrays79.4% acceptance

Given a square integer matrix of size n x n, rotate the matrix 90 degrees clockwise in-place. Modify the input matrix directly without allocating additional matrices.

Example 1

Input: [[10,20],[30,40]]

Output: [[30,10],[40,20]]

Explanation: 2x2 matrix rotated 90 degrees clockwise.

Example 2

Input: [[2,4,6],[8,10,12],[14,16,18]]

Output: [[14,8,2],[16,10,4],[18,12,6]]

Explanation: 3x3 matrix rotated 90 degrees clockwise.

Constraints

  • 1 <= len(matrix) <= 20
  • len(matrix) == len(matrix[0])
  • -1000 <= matrix[i][j] <= 1000 for all valid i, j
Python (current runtime)

Case 1

Input: [[1]]

Expected: [[1]]

Case 2

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

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

Case 3

Input: [[5,6],[7,8]]

Expected: [[7,5],[8,6]]

Case 4

Input: [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

Expected: [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]