Spiral Matrix

Traverse a 2D matrix in spiral order starting from the top-left corner.

Code

Algorithms
m = matrix
result = []
top, bottom, left, right = 0, len(m) - 1, 0, len(m[0]) - 1
while top <= bottom and left <= right:
    result.extend(m[top][left:right+1])
    top += 1
    for i in range(top, bottom + 1): result.append(m[i][right])
    right -= 1
    if top <= bottom:
        result.extend(m[bottom][left:right+1][::-1])
        bottom -= 1
    if left <= right:
        for i in range(bottom, top - 1, -1): result.append(m[i][left])
        left += 1
result

Parameters

2D matrix

Server

More Python Snippets