Skip to main content

LC 54 Spiral Matrix

// 54. Spiral Matrix
// Given an m x n matrix, return all elements of the matrix in spiral order.

const spiralOrder = matrix => {
if (!matrix.length || !matrix[0].length) return [];

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

while (top <= bottom && left <= right) {
for (let col = left; col <= right; col++) result.push(matrix[top][col]);
top++;

for (let row = top; row <= bottom; row++) result.push(matrix[row][right]);
right--;

if (top <= bottom) {
for (let col = right; col >= left; col--) result.push(matrix[bottom][col]);
bottom--;
}

if (left <= right) {
for (let row = bottom; row >= top; row--) result.push(matrix[row][left]);
left++;
}
}

return result;
};

console.log(spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]));
console.log(spiralOrder([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]));

export { spiralOrder };