Skip to content

top / bottom / left / right [terminology]

Terminology

Usage

  • Use full directional names when maintaining all four boundaries of a rectangular region.
  • Full names prevent ambiguity between row boundaries and column boundaries.
res = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
    # Traverse the current top edge.
    for c in range(left, right + 1):
        res.append(matrix[top][c])
    top += 1
    # ...

See 54. Spiral Matrix.