/** * * @param matrix int整型二维数组 * @return int整型一维数组 */ function spiralOrder( matrix ) { // write code here if(matrix.length === 0){ return [] } let left = 0 let right = matrix[0].length-1 let top = 0 let bottom = matrix.length-1 let res = [] let direction = "right" while(left<=right && top <= bottom){ if(direction === "right"){ for(let i = left;i<= right ;i++){ res.push(matrix[top][i]) } top++ direction = "down" }else if(direction === "down"){ for(let i = top;i<=bottom;i++){ res.push(matrix[i][right]) } right-- direction = 'left' }else if(direction === "left"){ for(let i = right;i>=left;i--){ res.push(matrix[bottom][i]) } bottom-- direction = "top" }else if(direction === "top"){ for(let i = bottom;i>=top;i--){ res.push(matrix[i][left]) } left++ direction = "right" } } return res } module.exports = { spiralOrder : spiralOrder };