题解 | #螺旋矩阵#
螺旋矩阵
https://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param matrix int整型二维数组 * @return int整型一维数组 */ function spiralOrder( arrs ) { if(!arrs.length || !arrs[0].length) return arrs let res = [] let status = true while (arrs.length) { if(status){ res = [...res,...arrs[0]] arrs.splice(0,1) if(arrs.length){ arrs.forEach((element,index) => { res.push(element[element.length-1]) arrs[index].pop() }); } status = !status }else{ res = [...res,...arrs.pop().reverse()] if(arrs.length){ let temp = [] arrs.forEach((element,index)=> { temp.push(element[0]) arrs[index].splice(0,1) }) temp = temp.reverse() res = [...res,...temp] } status = !status } arrs = arrs.filter(item => item.length>0) } return res } module.exports = { spiralOrder : spiralOrder };