function printMatrix( matrix ) { // write code here const res = [] if(matrix.length==0) return [] let top = 0,bottom = matrix.length-1,right= matrix[0].length-1,left=0 while(left<right&&top<bottom){ for(let i= left;i<right;i++) res.push(matrix[top][i])//上层 for(let i = top;i<bottom;i++) res.push(matrix[i][right])//右层 for(let i = right;i>left;i--) res.push(matrix[bottom][i])//下层 for(let i = bottom;i>top;i--) res.push(matrix[i][left])//左层 top ++ bottom-- left++ right-- } if(left==right){ for(let i = top;i<=bottom;i++) res.push(matrix[i][right])//右层 } else if(top == bottom){ for(let i= left;i<=right;i++) res.push(matrix[top][i])//上层 } return res }