/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 a new matrix. Return the modified matrix.

Example 1

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

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

Explanation: After rotating 90 degrees clockwise, the first column becomes the first row.

Example 2

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

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

Explanation: Each layer is rotated in-place.

Constraints

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

Case 1

Input: [[2,4],[6,8]]

Expected: [[6,2],[8,4]]

Case 2

Input: [[11,12,13],[14,15,16],[17,18,19]]

Expected: [[17,14,11],[18,15,12],[19,16,13]]

Case 3

Input: [[5]]

Expected: [[5]]

Case 4

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

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