Dashboard
#68. Rotate ImageMediumMath & Geometry

Given a square n x n matrix of integers matrix, rotate it by 90 degrees clockwise.

You must rotate the matrix in-place. Do not allocate another 2D matrix and do the rotation.

Example 1:

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

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

Example 2:

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

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

Constraints:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
Examples
Example 1:
Input: matrix = [ [1,2], [3,4] ]
Output: [ [3,1], [4,2] ]
Example 2:
Input: matrix = [ [1,2,3], [4,5,6], [7,8,9] ]
Output: [ [7,4,1], [8,5,2], [9,6,3] ]
Constraints
  • `n == matrix.length == matrix[i].length`
  • `1 <= n <= 20`
  • `-1000 <= matrix[i][j] <= 1000`
Company Frequency
Cisco18IBM15Google13Amazon9Zoho9Roblox8Capital One8Infosys6
Target: O(N) time • O(1) spaceArray / Math / Matrix
Socratic AI Tutor
Gemini 3.8 Flash
Loading editor...

Click "Run" or press Ctrl+Enter to execute test cases.