题目
https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/
思路

按照这个图片的思路 先
从左—->右,
从上—->下,
从右—->左,
从下—->上
同时要注意收缩边界
代码
public int[] spiralOrder(int[][] matrix) {if(matrix.length == 0) return new int[0];int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1, x = 0;int[] res = new int[(right + 1) * (bottom + 1)];while(true) {for(int i = left; i <= right; i++) res[x++] = matrix[top][i]; // left to right.//每次循环top都加一,如果top比bottom大说明已经遍历完成if(++top > bottom) break;for(int i = top; i <= bottom; i++) res[x++] = matrix[i][right]; // top to bottom.//因为是向内遍历,所以每次right都减1if(left > --right) break;for(int i = right; i >= left; i--) res[x++] = matrix[bottom][i]; // right to left.if(top > --bottom) break;for(int i = bottom; i >= top; i--) res[x++] = matrix[i][left]; // bottom to top.if(++left > right) break;}return res;}
