54. 螺旋矩阵(模拟,边界问题)

给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

解法

转圈遍历,模拟转圈;

1.模拟转圈

思路:如题目中的图一样转圈,先从1-3,再6-9,再8-7 再4 再5;

当一个步骤之后,遍历结束时, 这个步骤后判断越界 来结束遍历;

public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<Integer>();
        
        int n = matrix.length, m = matrix[0].length;
        int up = 0, left = 0, down = n-1, right = m-1;
        while(true)
        {
            for(int i = left; i <= right; ++i) res.add(matrix[up][i]); //1-3   5
            if(++up>down) break; 
            for(int i = up; i <= down; ++i) res.add(matrix[i][right]); //6-9
            if(--right<left) break;
            for(int i = right; i>=left; --i) res.add(matrix[down][i]); //8-7 
            if(--down<up) break;
            for(int i = down; i>= up; --i) res.add(matrix[i][left]); //4 
            if(++left>right)break;
        }
        return res;
    }

2.优化: 可以不用每一个步骤后都判断越界,因为按照遍历的规则,遍历完成前,规则都不会产生越界,只有遍历了所有数之后,才会越界

 public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<Integer>();
        
        int n = matrix.length, m = matrix[0].length , count = n*m;
        int up = 0, left = 0, down = n-1, right = m-1;
        while(count>=1)
        {
            for(int i = left; i <= right && count>=1 ; ++i) {res.add(matrix[up][i]); count--;}
            up++;
            for(int i = up; i <= down && count>=1 ; ++i) {res.add(matrix[i][right]);count--;}  
            right--;
            for(int i = right; i>=left && count>=1 ; --i) {res.add(matrix[down][i]);count--;}
            down--;
            for(int i = down; i>= up && count>=1 ; --i){ res.add(matrix[i][left]);count--;}
            left++;
        }
        return res;
    }

时间复杂度:O(mn),其中 m 和 n 分别是输入矩阵的行数和列数。矩阵中的每个元素都要被访问一次。

空间复杂度:O(1)。
3.简单易懂版本
加个visited数组,遍历步骤遇到访问过的元素就是越界,遍历结束。

2.按层遍历

一圈一圈遍历,

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;
    }

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/spiral-matrix/solution/luo-xuan-ju-zhen-by-leetcode-solution/
来源:力扣(LeetCode)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值