Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Spiral Matrix

XPChallenge Points: 20
levelLevel: Medium

You are given a 2D array matrix of size m x n.
Return all the elements of the matrix in a spiral (clockwise) order, starting from the top-left corner and moving inward layer by layer.

Example 1:

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

Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input: matrix = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]

Output: [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result