力扣 54. 螺旋矩阵

直接模拟顺时针螺旋遍历
主要要解决的问题就是转圈时,在什么情况下转弯
最直接的办法就是利用一个辅助矩阵记录已经遍历过的数字,但这种方法空间复杂度会比较差
我的优化想法是,记录四条边已经遍历过的宽度,例如,当由向右转向为向下时,上边被遍历宽度就加一;每当转向时都有一条边加一。总之达到一个不断缩小可遍历矩阵的效果。
官方题解是采用了按层模拟的想法,可以将矩阵看成若干层,首先输出最外层的元素,其次输出次外层的元素,直到输出最内层的元素。
[[1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 1],
[1, 2, 3, 3, 3, 2, 1],
[1, 2, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1]]

自己的(0ms) 可以看到分为四个方向去讨论的代码太冗长了,虽然时间复杂度不受影响

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> ans = new ArrayList<Integer>();
        int to = 4, x = 0, y = 0;
        int r = 0,l = 0, u = 0, d = 0;
        ans.add(matrix[0][0]);
        int jud = 1;
        while(jud == 1){
            switch(to){
                case 1:
                    if(x - 1 == u - 1){
                        if(y + 1 == matrix[0].length - r){
                            jud = 0;
                            break;
                        }
                        to = 4;
                        l ++;
                        y ++;
                    }
                    else
                        x--;
                    ans.add(matrix[x][y]);
                    break;
                case 2:
                    if(x + 1 == matrix.length - d){
                        if(y - 1 == l - 1){
                            jud = 0;
                            break;
                        }
                        to = 3;
                        r ++;
                        y --;
                    }
                    else
                        x++;
                    ans.add(matrix[x][y]);
                    break;
                case 3:
                    if(y - 1 == l - 1){
                        if(x - 1 == u - 1){
                            jud = 0;
                            break;
                        }
                        to = 1;
                        d ++;
                        x --;
                    }
                    else
                        y--;
                    ans.add(matrix[x][y]);
                    break;
                case 4:
                    if(y + 1 == matrix[0].length - r){
                        if(x + 1 == matrix.length - d){
                            jud = 0;
                            break;
                        }
                        to = 2;
                        u ++;
                        x ++;
                    }
                    else
                        y++;
                    ans.add(matrix[x][y]);
                    break;
            }
        }
        return ans;
    }
}

题解(0ms)

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> order = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return order;
        }
        int rows = matrix.length, columns = matrix[0].length;
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        while (left <= right && top <= bottom) {
            for (int column = left; column <= right; column++) {
                order.add(matrix[top][column]);
            }
            for (int row = top + 1; row <= bottom; row++) {
                order.add(matrix[row][right]);
            }
            if (left < right && top < bottom) {
                for (int column = right - 1; column > left; column--) {
                    order.add(matrix[bottom][column]);
                }
                for (int row = bottom; row > top; row--) {
                    order.add(matrix[row][left]);
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eyvr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值