Skip to content
Onsite

Matrix Boundary Traversal

EasyP3Coding

matrix is an N x N matrix. Return its boundary elements in clockwise order, starting at the bottom-left corner: go up the first column, then right along the top row, then down the last column, then left along the bottom row, stopping before the starting corner. Every boundary element appears exactly once.

For a 1 x 1 matrix the answer is its single element.

Example 1

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

Example 2

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

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.