11 · Matrices & Simulation

#48 Rotate Image

Mediumsolve on LeetCode ↗

frequently reported atMicrosoftAppleBloombergAmazonGoogleMeta

the problem

In plain words

Rotate an n × n matrix 90° clockwise, in place — do not allocate another matrix.

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

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

in matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

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

  • n == matrix.length == matrix[i].length
  • 1 ≤ n ≤ 20
  • -1000 ≤ matrix[i][j] ≤ 1000

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #54
      Spiral Matrix
      Mediumin Matrices
    2. #189
      Rotate Array
      Mediumin Arrays & Strings