InferenceAI

Enter at least two characters to search.

Start free

Spiral Order

mediumArrays & Strings25 min25 XP

Return all elements of the m×nm \times n matrix in clockwise spiral order, starting at the top-left.

Examples

EXAMPLE WALKTHROUGHsquare

A guided view of the published example, not a trace of your code.

INPUT[[[1,2,3],[4,5,6],[7,8,9]]]
TASKspiral_order()
EXPECTED OUTPUT[1,2,3,6,9,8,7,4,5]

Start with the input values shown in this example.

Example 1 of 3

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

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

Explanation: square

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

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

Explanation: wide

Input: matrix = [[1],[2],[3]]

Output: [1,2,3]

Explanation: single column

Hint 1

Track four boundaries (top, bottom, left, right) and shrink them after each side.

Topics

DSAArrays & StringsPython