Spiral Matrix in TypeScript

function spiralOrder(matrix: number[][]): number[] {
    if (matrix.length === 0 || matrix[0].length === 0) return [];

    const result: number[] = [];
    let top = 0, bottom = matrix.length - 1;
    let left = 0, right = matrix[0].length - 1;

    while (top <= bottom && left <= right) {
        // Traverse from left to right along the top row
        for (let i = left; i <= right; i++) result.push(matrix[top][i]);
        top++;

        // Traverse from top to bottom along the right column
        for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
        right--;

        // Check if we are done
        if (top > bottom || left > right) break;

        // Traverse from right to left along the bottom row
        for (let i = right; i >= left; i--) result.push(matrix[bottom][i]);
        bottom--;

        // Traverse from bottom to top along the left column
        for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
        left++;
    }

    return result;
}

PrevNext