Problems > Spiral Order > Editorial

Spiral Order — Solution & Editorial

Back to the Problem

Peel layer by layer with four boundary indices; guard the single-row and single-column cases inside a layer.

Complexity: O(NM)

Watch out: A layer that is a single row or column gets traversed twice without the two inner guards.