Skip to the code
+25 XP

#78 Rotate the Canvas Clockwise

medium

A pixel editor has a "rotate 90 degrees clockwise" button for square canvases. The canvas is grid, an n x n array of pixel values.

Write rotateCanvas(grid) that rotates grid in place: after the call, grid itself holds the rotated canvas, where the old left column (bottom to top) is the new top row. Don't build a second n x n array.

Example 1

Input: grid = [[1,2],[3,4]]
Output: [[3,1],[4,2]]

The left column, read bottom to top, is 3, 1, which becomes the top row.

Example 2

Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
Output: [[1,1,1],[1,1,0],[1,0,0]]

The staircase that rose to the right now steps down to the right.

Constraints

  • 1 <= n <= 2000
  • -1000 <= grid[r][c] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).