Skip to main content
PreparationZone Logo
PreparationZone
Back to Coding

Coding · Medium · Pro

Rotate an n×n matrix 90 degrees clockwise in-place

CodingMatrixSenior
Model answerPreview

Approach

  • Transpose the matrix, then reverse each row.
  • Or rotate layer by layer in place for strict O(1) extra space.

Java Solution

Java
public void rotate(int[][] matrix) {
    int n = matrix.length;…