Skip to content
Onsite

N-Shape Matrix Traversal

EasyP3Coding

Return the elements of an N x N matrix in an N shape, as one array:

  1. The first column from the bottom up, stopping before the top-left cell.
  2. The main diagonal from the top-left cell to the bottom-right cell.
  3. The last column from the cell above the bottom-right corner up to the top.

Every visited cell appears once.

Example 1

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

Example 2

Input
matrix = [[1,2],[3,4]]
Output
[3,1,4,2]

Example 3

Input
matrix = [[5]]
Output
[5]

Constraints

  • 1 <= N <= 100
  • -10^9 <= matrix[i][j] <= 10^9

Hints

Run checks the 3 visible tests, Submit all 10.