题解 | #螺旋矩阵#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
*
* @param matrix int整型二维数组
* @return int整型一维数组
*/
function spiralOrder( matrix ) {
// write code here
let res = [];
let flag = true;
while(matrix.length) {
if(flag) {
res = [...res, ...matrix.shift()];
if(matrix[0] && matrix[0].length) {
for(let i=0;i<matrix.length;i++) {
res.push(matrix[i].pop())
}
}
flag = false;
} else {
res = [...res, ...matrix.pop().reverse()];
if(matrix[0] && matrix[0].length) {
for(let i=matrix.length-1;i>=0;i--) {
res.push(matrix[i].shift())
}
}
flag = true;
}
}
return res;
}
module.exports = {
spiralOrder : spiralOrder
};