Skip to content
BytePatterns

Rotate A Square Grid

MediumMatrix & Grid#in-place#transpose-reverse~20m

Problem

Turn an n by n grid a quarter turn clockwise, so the first column read from the bottom up becomes the first row. Do it in place, rearranging values inside the grid you were given rather than building a second grid.

Examples

Input:  grid = [[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]]
Output: [[7, 4, 1],
         [8, 5, 2],
         [9, 6, 3]]
Input:  grid = [[1, 2],
                [3, 4]]
Output: [[3, 1],
         [4, 2]]
Input:  grid = [[7]]
Output: [[7]]
Why:    edge case, a single cell turns into itself

Hints

0 / 3

Stuck on the idea rather than the code? Rotate In Place covers it.