Leetcode54螺旋打印矩阵

用左神一句话,就是要有宏观思想,不要去想怎么一步步走,扣边界,很多数组旋转或者什么形状打印,都宏观想一下,这个题是一圈一圈的往里打印。

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res=new LinkedList();
        if(matrix.length==0||matrix[0].length==0){
            return res;
        }
        int row1=0;
        int col1=0;
        int row2=matrix.length-1;
        int col2=matrix[0].length-1;
        
        while(row1<=row2&&col1<=col2){
            printEdge(matrix,row1++,col1++,row2--,col2--,res);
        }
        return res;
    }
    public void printEdge(int[][] matrix,int row1,int col1,int row2,int col2,List<Integer> res){
        if(row1==row2){
            while(col1<=col2){
                res.add(matrix[row1][col1++]);
            }
        }else if(col1==col2){
            while(row1<=row2){
                res.add(matrix[row1++][col1]);
            }
        }else{
            int col11=col1;
            int row11=row1;
            while(col11<col2){
                res.add(matrix[row1][col11++]);
            }
            while(row11<row2){
                res.add(matrix[row11++][col11]);
            }
            while(col11>col1){
                res.add(matrix[row11][col11--]);
            }
            while(row11>row1){
                res.add(matrix[row11--][col11]);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值