这道题并没有用到什么复杂的算法框架,注意后两个条件要加以判断

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix==null||matrix.length==0||matrix[0].length==0)return new int[0];
        int row = matrix.length;
        int col = matrix[0].length;
        int[] res = new int[row*col];
        int index = 0;
        int top = 0;
        int bottom = row-1;
        int right = col-1;
        int left = 0;
        while(bottom>=top&&left<=right){
            for(int i=left;i<=right;i++)res[index++]=matrix[top][i];
            for(int i=top+1;i<=bottom;i++)res[index++]=matrix[i][right];
            if(left<right&&top<bottom){
            for(int i=right-1;i>=left;i--)res[index++]=matrix[bottom][i];
            for(int i=bottom-1;i>top;i--)res[index++]=matrix[i][left];
            }
            top++;
            bottom--;
            left++;
            right--;
        }
        return res;
    }
}

Q.E.D.