Source: https://leetcode.com/problems/spiral-matrix/
Given an m x n matrix, return all elements of the matrix in spiral order.
/*** @param {number[][]} matrix* @return {number[]}*/// Approach: Go clockwise while making sure to close the bounds// While we are still in bounds we will go through elements in a clockwise fashion and add to result along the way// We will iterate right from the left bound (left bound to right bound); increment upper bound// We will iterate down from the right bound (upper bound to bottom bound); decrement right bound// Check the left/right bound and upper/bottom bounds are still valid to go left and up// We will iterate left from right bound (right bound to left bound); decrement bottom bound// We will iterate up from bottom bound (bottom bound to upper bound); increment left bound// O(mn) timevar spiralOrder = function(matrix) {const spiralResult = [];let leftBound = 0;let rightBound = matrix[0].length - 1;let upperBound = 0;let bottomBound = matrix.length - 1;// While we are in bounds, we will go through elements in a clockwise fashion and add to result along the waywhile (leftBound <= rightBound && upperBound <= bottomBound) {// Go right from the upper left boundfor (let col = leftBound; col <= rightBound; col++) {spiralResult.push(matrix[upperBound][col]);}// Move upper bound inwardsupperBound++;// Go down from the upper right boundfor (let row = upperBound; row <= bottomBound; row++) {spiralResult.push(matrix[row][rightBound]);}// Move right bound inwardsrightBound--;// Check if left/right bound and upper/bottom bound are valid to go to avoid duplicate traversalif (leftBound > rightBound || upperBound > bottomBound) {break;}// Go left from bottom right boundfor (let col = rightBound; col >= leftBound; col--) {spiralResult.push(matrix[bottomBound][col]);}// Move bottom bound inwardsbottomBound--;// Go up from bottom left boundfor (let row = bottomBound; row >= upperBound; row--) {spiralResult.push(matrix[row][leftBound]);}// Move left bound inwardsleftBound++;}return spiralResult;};