Deviopath › Python

Rotate a Matrix 90 Degrees

Medium · Lists

A matrix is a list of rows. zip(*matrix) transposes it: the first items of every row become the first new row, and so on. Reverse the rows first (matrix[::-1]) and the transpose becomes a clockwise rotation.

Write rotate(matrix) that returns a new matrix rotated 90 degrees clockwise, as a list of lists. The matrix may be rectangular (a 2x3 matrix becomes 3x2). An empty matrix gives []. Do not change the input.

Next: Two Sum