// 先看II,然后I就秒改
public class Solution {
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(matrix==null || matrix.length==0 || matrix[0].length==0) return res;
int left=0, right = matrix[0].length-1, top = 0, bottom = matrix.length-1;
while(left<right && top<bottom){
for(int i=left; i< right; i++){
res.add(matrix[top][i]);
}
for(int j=top;j<bottom; j++){
res.add(matrix[j][right]);
}
for(int x = right; x>left; x--){
res.add(matrix[bottom][x]);
}
for(int y = bottom; y>top; y--){
res.add(matrix[y][left]);
}
left++;
right--;
bottom--;
top++;
}
if(left==right) {
for(int p = top; p<=bottom;p++) res.add(matrix[p][left]);
}else if(top==bottom){
for(int q = left; q<=right;q++) res.add(matrix[top][q]);
}
return res;
}
}