LeetCode54.Spiral Matrix

LeetCode:Topic Link

主函数分圈处理,封装函数每一圈的处理过程。

 ​​

1.主函数标记左上角坐标(tx,ty),右下角坐标(dx,dy)。

每一圈的处理相同,因此循环分圈,直至(tx <= dx) && (ty <= dy)

    public List<Integer> spiralOrder(int[][] matrix) {
        //左上角元素坐标
        int tx = 0;
        int ty = 0;
        //右上角元素坐标
        int dx = matrix.length - 1;
        int dy = matrix[0].length - 1;
        //初始化一个List用于存储遍历后的数据
        List<Integer> res = new ArrayList<>();
        while((tx <= dx) && (ty <= dy)){
           OneSpiral(tx, ty, dx, dy, matrix, res);
            tx++;
            ty++;
            dx--;
            dy--;
        }
        return res;
    }

2.每圈详细处理函数

    public List<Integer> OneSpiral(int tx, int ty, int dx, int dy, int[][] matrix, List<Integer> res){
        int curx = tx;
        int cury = ty;
        //如果只有一行
        if(ty == dy){
            while(curx <= dx){
                res.add(matrix[curx][cury]);
                curx++;
            }
        }
        //如果只有一列
        else if(tx == dx){
            while(cury <= dy){
                res.add(matrix[curx][cury]);
                cury++;
            }
        }else{
            //顺时针打印
            while(cury < dy){
                res.add(matrix[curx][cury]);
                cury++;
            }
            while(curx < dx){
                res.add(matrix[curx][cury]);
                curx++;
            }
            while(cury > ty){            
                res.add(matrix[curx][cury]);
                cury--;
            }
            while(curx > tx){
                res.add(matrix[curx][cury]);
                curx--;
            }
        }
        
        return res;
    }

3.完整程序

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        //左上角元素坐标
        int tx = 0;
        int ty = 0;
        //右上角元素坐标
        int dx = matrix.length - 1;
        int dy = matrix[0].length - 1;
        //初始化一个List用于存储遍历后的数据
        List<Integer> res = new ArrayList<>();
        while((tx <= dx) && (ty <= dy)){
           OneSpiral(tx, ty, dx, dy, matrix, res);
            tx++;
            ty++;
            dx--;
            dy--;
        }
        return res;
    }
    public List<Integer> OneSpiral(int tx, int ty, int dx, int dy, int[][] matrix, List<Integer> res){
        int curx = tx;
        int cury = ty;
        //如果只有一行
        if(ty == dy){
            while(curx <= dx){
                res.add(matrix[curx][cury]);
                curx++;
            }
        }
        //如果只有一列
        else if(tx == dx){
            while(cury <= dy){
                res.add(matrix[curx][cury]);
                cury++;
            }
        }else{
            //顺时针打印
            while(cury < dy){
                res.add(matrix[curx][cury]);
                cury++;
            }
            while(curx < dx){
                res.add(matrix[curx][cury]);
                curx++;
            }
            while(cury > ty){            
                res.add(matrix[curx][cury]);
                cury--;
            }
            while(curx > tx){
                res.add(matrix[curx][cury]);
                curx--;
            }
        }
        
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值