import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res = new ArrayList<>();
if(matrix.length == 0){
return res;
}
int left = 0;
int right = matrix[0].length -1;
int top = 0;
int bottom = matrix.length -1;
while(left < (matrix[0].length + 1)/2 && top < (matrix.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;i>=left && bottom != top;i--){
res.add(matrix[bottom][i]);
}
//从下到上
for(int i = bottom - 1;i>top && left!= right;i--){
res.add(matrix[i][left]);
}
++left;
++top;
--right;
--bottom ;
}
return res;
}
}