javascript
var spiralOrder = function(matrix) {
let ans=[];
let left =0,right=matrix[0].length-1;
let top=0,bottom=matrix.length-1;
while(left<right&&top<bottom){
for(let i=left;i<right;i++) ans.push(matrix[top][i]);
for(let i=top;i<bottom;i++) ans.push(matrix[i][right]);
for(let i=right;i>left;i--) ans.push(matrix[bottom][i]);
for(let i=bottom;i>top;i--) ans.push(matrix[i][left]);
left++,right--;
top++,bottom--;
}
if(top==bottom){
for(let i=left;i<=right;i++) ans.push(matrix[top][i]);
}
else if(left==right){
for(let i=top;i<=bottom;i++) ans.push(matrix[i][left]);
}
return ans;
};
算法核心:直接见图
