(leetcode)力扣100 19螺旋矩阵(方向数组/边界把控)

题目

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

数据范围

java 复制代码
m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100

测试用例

示例1

java 复制代码
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例2

java 复制代码
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

题解(博主思路,时间O(nm),空间O(1))

java 复制代码
class Solution {
    static int dire[][]={{0,1,0,-1},{1,0,-1,0}};
    static int direpos;
    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res=new ArrayList<>();
        direpos=0;
        int m=matrix.length;
        int n=matrix[0].length;
        int tm=m;
        int tn=n;
        int x=-1;
        int y=0;
        int all=n*m;
        for(int i=0;i<all;i++){
            x=x+dire[1][direpos];
            y=y+dire[0][direpos];
            res.add(matrix[y][x]);
            if(direpos==0||direpos==2){
                tn--;
                if(tn==0){
                    direpos=(direpos+1)%4;
                    tn=n-1;
                    n=n-1;
                    tm--;
                }
            }else{
                tm--;
                if(tm==0){
                    direpos=(direpos+1)%4;
                    tm=m-1;
                    m=m-1;
                }
            }
        }
        return res;
    }
}

官解1(时间O(NM),空间O(NM))

java 复制代码
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;
        boolean[][] visited = new boolean[rows][columns];
        int total = rows * columns;
        int row = 0, column = 0;
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int directionIndex = 0;
        for (int i = 0; i < total; i++) {
            order.add(matrix[row][column]);
            visited[row][column] = true;
            int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
}

官解2(时间O(nm),空间O(1))

java 复制代码
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;
    }
}

思路

这道题算是一道简单的题,因为他的思路很简单,做法无非两种,第一个官解方法一,通过预定方法来做,第二个方法无非就是通过几个变量来维护方向,也就是官解的第二种解法。思路都不难,这道题难度在细节处理,边界处理容易出问题。博主的方法采用了方向数组,但不想官解1使用了记忆数组来存储走过的路,而是通过(tn,tm)两个变量维护剩余步数,来达到节约空间的目的。

综上,其实官解的方法二还是更推荐一些的,虽然博主方法的空间复杂度同样是O(1),但博主的方法边界处理更麻烦。博主的思路是,横向移动时,横向的可移动距离-1,纵向的可移动距离也要-1,纵向移动到底时,只有纵向的可移动距离-1。然后维护每次定向移动的可移动距离即可。

说实话博主的方法和官方方法二思路其实大相径庭,一个是在每一次行动中维护边界,一个在运行一圈后总的维护边界,仁者见仁,智者见智吧。

相关推荐
lbb 小魔仙19 分钟前
面向 NPU 的高性能矩阵乘法:CANN ops-nn 算子库架构与优化技术
线性代数·矩阵·架构
空白诗27 分钟前
CANN ops-nn 算子解读:大语言模型推理中的 MatMul 矩阵乘实现
人工智能·语言模型·矩阵
Wei&Yan1 小时前
数据结构——顺序表(静/动态代码实现)
数据结构·c++·算法·visual studio code
团子的二进制世界1 小时前
G1垃圾收集器是如何工作的?
java·jvm·算法
吃杠碰小鸡1 小时前
高中数学-数列-导数证明
前端·数学·算法
故事不长丨1 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
long3161 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
近津薪荼1 小时前
dfs专题4——二叉树的深搜(验证二叉搜索树)
c++·学习·算法·深度优先
熊文豪2 小时前
探索CANN ops-nn:高性能哈希算子技术解读
算法·哈希算法·cann
熊猫_豆豆2 小时前
YOLOP车道检测
人工智能·python·算法