LeetCode — Rotate Image (Blind Curated)

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Example 2:


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


Time complexity: O(N), each index value in matrix is read and written once.

Space complexity: O(1), no matrix variable is being declared or created, but used matrix recursively.

LeetCode — Rotate Image (Blind Curated 2/75)




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store