54. Spiral Matrix

public class Solution {
    public ListInteger spiralOrder(int[][] matrix) {
        ListInteger res=new ArrayListInteger();
        if(matrix.length==0||matrix[0].length==0)
            return res;
        spiralOrder(0, 0, matrix.length-1, matrix[0].length-1, res, matrix);
        return res;
    }
    private void spiralOrder(int top, int left, int bottom, int right, ListInteger res, int[][] matrix)
    {
        if(topbottom||leftright)
            return;
        for(int k=left;k=right;k++)
            res.add(matrix[top][k]);
        for(int k=top+1;k=bottom;k++)
            res.add(matrix[k][right]);
        if(top!=bottom)
            for(int k=right-1;k=left;k--)
                res.add(matrix[bottom][k]);
        if(left!=right)
            for(int k=bottom-1;ktop;k--)
                res.add(matrix[k][left]);
        spiralOrder(top+1, left+1, bottom-1, right-1, res, matrix);
    }
}

 

转载于:https://www.cnblogs.com/asuran/p/7594709.html

最新回复(0)
/jishu1262SZf3ToRlIrJ4OsXL7mOYIemRfQ49Tpbohg_3D_3D4489372
8