《LeetCode热题100》---<6.①矩阵四道(二维数组)>

本篇博客讲解LeetCode热题100道矩阵篇中的四道题

第一道:矩阵置零(中等)

第二道:螺旋矩阵(中等)

第一道:矩阵置零(中等)

方法一:使用标记数组

java 复制代码
class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean[] row = new boolean[m];
        boolean[] col = new boolean[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    row[i] = col[j] = true;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j]) {
                    matrix[i][j] = 0;
                }
            }
        }
    }
}

算法思路

用两个标记数组分别记录每一行和每一列是否有零出现。

步骤:

1.首先获取矩阵数组的长度为m,也就是m行。记列的个数为n。也就是matrix[0].length;

2.遍历该数组一次,如果某个元素为 0,那么就将该元素所在的行和列所对应标记数组的位置置为 true。

3.再次遍历该数组,如果row[i] || col[j]为真,则matrix[i][j] = 0;
复杂度分析

**时间复杂度:**O(mn),其中 m 是矩阵的行数,n 是矩阵的列数。我们至多只需要遍历该矩阵两次。

**空间复杂度:O(m+n):**其中 m 是矩阵的行数,n 是矩阵的列数。我们需要分别记录每一行或每一列是否有零出现。

第二道:螺旋矩阵(中等)

方法一:模拟

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

1.模拟螺旋矩阵的路径。初始位置是矩阵的左上角,初始方向是向右,当路径超出界限或者进入之前访问过的位置时,顺时针旋转,进入下一个方向。

2.判断路径是否进入之前访问过的位置需要使用一个与输入矩阵大小相同的辅助矩阵 visited,其中的每个元素表示该位置是否被访问过。

3.当一个元素被访问时,将 visited 中的对应位置的元素设为已访问。

4.当路径的长度达到矩阵中的元素数量时即为完整路径,将该路径返回。
复杂度分析

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

**空间复杂度:**O(mn)。需要创建一个大小为 m×n 的矩阵 visited 记录每个位置是否被访问过。

方法二:按层模拟

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

算法思想:

可以将矩阵看成若干层,首先输出最外层的元素,其次输出次外层的元素,直到输出最内层的元素。

复杂度分析

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

**空间复杂度:**O(1)。除了输出数组以外,空间复杂度是常数。

相关推荐
小字节,大梦想8 分钟前
【数据结构】详细介绍各种排序算法,包含希尔排序,堆排序,快排,归并,计数排序
c语言·数据结构·c++·算法
程序猿进阶8 分钟前
ThreadLocal 释放的方式有哪些
java·开发语言·性能优化·架构·线程池·并发编程·threadlocal
战族狼魂11 分钟前
java代码 识别pdf文件是否含有表格
java·python·pdf
Mryan200515 分钟前
OpenJudge | 寻找中位数
开发语言·数据结构·c++·算法·openjudge
码里法1 小时前
springmvc用配置类替换xml配置
java·spring·mvc
api茶飘香2 小时前
守护应用边界:通过反射API实现安全的输入输出过滤
java·开发语言·python·安全·django·virtualenv·pygame
杀死一只知更鸟debug2 小时前
策略模式的小记
java·开发语言·策略模式
nice666602 小时前
CSS的基本语法
java·前端·css·visual studio code
七十二五2 小时前
matlab数据批量保存为excel,文件名,行和列的名称设置
经验分享·算法·matlab·青少年编程·矩阵·excel
阿巴~阿巴~2 小时前
C_深入理解指针(五) —— sizeof和strlen的对比、数组和指针笔试题解析、指针运算笔试题解析
c语言·开发语言·数据结构·算法