import java.util.*;
// 1 2 3 螺旋 1 2 3 6 9 8 7 4 5
// 4 5 6
// 7 8 9
public class Solution {
public ArrayList<integer> spiralOrder(int[][] matrix) {
ArrayList<integer> res = new ArrayList<>();
if(matrix.length == 0)
return res;
// 定义四个指针,并且充当边界限制的作用
int top = 0, bottom = matrix.length-1;
int left = 0, right = matrix[0].length-1;
while( top < (matrix.length+1)/2 && left < (matrix[0].length+1)/2 ){
//上面 左到右
for(int i = left; i <= right; i++){
res.add(matrix[top][i]);
}
//右边 上到下
for(int i = top+1; i <= bottom; i++){
res.add(matrix[i][right]);
}
//下面 右到左
for(int i = right-1; top!=bottom && i>=left; i--){
res.add(matrix[bottom][i]);
}
//左边 下到上
for(int i = bottom-1; left!=right && i>=top+1; i--){
res.add(matrix[i][left]);
}
// 遍历完一圈之后,所有往里面靠
++top;
--bottom;
++left;
--right;
}
return res;
}
}