矩阵【Lecode_HOT100】

1.矩阵置零
java 复制代码
  public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        boolean firstRowZero = false; // 标记第一行是否需要置为 0
        boolean firstColZero = false; // 标记第一列是否需要置为 0

        // 第一次遍历:记录需要置零的行和列
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    if (i == 0) firstRowZero = true;
                    if (j == 0) firstColZero = true;
                    matrix[i][0] = 0; // 标记所在行
                    matrix[0][j] = 0; // 标记所在列
                }
            }
        }

        // 第二次遍历:根据标记将相应的元素置为 0
        for (int i = 1; i < m; i++) { // 注意从 1 开始,避免覆盖标记行/列
            for (int j = 1; j < n; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }

        // 处理第一行
        if (firstRowZero) {
            for (int j = 0; j < n; j++) {
                matrix[0][j] = 0;
            }
        }

        // 处理第一列
        if (firstColZero) {
            for (int i = 0; i < m; i++) {
                matrix[i][0] = 0;
            }
        }
    }
2.螺旋矩阵
java 复制代码
 public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return result;
        }

        int top = 0, bottom = matrix.length - 1;
        int left = 0, right = matrix[0].length - 1;

        while (top <= bottom && left <= right) {
            // 从左到右遍历上边界
            for (int i = left; i <= right; i++) {
                result.add(matrix[top][i]);
            }
            top++;

            // 从上到下遍历右边界
            for (int i = top; i <= bottom; i++) {
                result.add(matrix[i][right]);
            }
            right--;

            // 从右到左遍历下边界
            if (top <= bottom) {
                for (int i = right; i >= left; i--) {
                    result.add(matrix[bottom][i]);
                }
                bottom--;
            }

            // 从下到上遍历左边界
            if (left <= right) {
                for (int i = bottom; i >= top; i--) {
                    result.add(matrix[i][left]);
                }
                left++;
            }
        }

        return result;
    }
3.旋转图像No.48
java 复制代码
public void rotate(int[][] matrix) {
        /*思路:
        1.对角线互换
        2.中线互换
        */
        int n = matrix.length;
        //对角线互换
        for(int i = 0;i<n;i++){
            for(int j = i+1;j<n;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        //中线互换
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n/2;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][n-j-1];
                matrix[i][n-j-1] = temp;
            }
        }
    }
4.搜素二维矩阵IINo.240
  • 暴力法
java 复制代码
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(matrix[i][j]==target) return true;
            }
        }
        return false;
    }
}
  • 从矩阵的右上角或左下角开始搜素
    • 右上角:若当前元素大于目标值,则往左移动;若小于目标值,则往下移动。
    • 左上角:若当前元素大于目标值,则往上移动;若小于目标值,则往右移动。
  • 算法步骤
    • 初始化起点为矩阵的右上角 (0, n-1),其中 n 是列数。
    • 循环条件:当前行号和列号都在矩阵范围内。
    • 比较当前元素与目标值:
      • 如果当前元素等于目标值,返回 true
      • 如果当前元素大于目标值,向左移动。
      • 如果当前元素小于目标值,向下移动。
    • 如果遍历完整个矩阵仍未找到目标值,返回 false
java 复制代码
 public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        if(matrix==null||m==0||n==0){
            return false;
        }
        //定义右上角
        int row = 0;
        int col = n-1;
        while(row<m&&col>=0){
            if(matrix[row][col]==target){
                return true;
            }else if(matrix[row][col]<target){
                row++;
            }else{
                col--;
            }
        }
        return false;
    }
相关推荐
hn小菜鸡4 小时前
LeetCode 3643.垂直翻转子矩阵
算法·leetcode·矩阵
Chan165 小时前
JVM从入门到实战:从字节码组成、类生命周期到双亲委派及打破双亲委派机制
java·jvm·spring boot·后端·intellij-idea
张晓~183399481215 小时前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
招风的黑耳5 小时前
Java生态圈核心组件深度解析:Spring技术栈与分布式系统实战
java·spring·wpf
ゞ 正在缓冲99%…5 小时前
leetcode101.对称二叉树
算法
zhangyifang_0095 小时前
泛型通配符 T、E、K、V、?
java
YuTaoShao6 小时前
【LeetCode 每日一题】3000. 对角线最长的矩形的面积
算法·leetcode·职场和发展
四谎真好看6 小时前
Java 黑马程序员学习笔记(进阶篇6)
java·笔记·学习·学习笔记
2zcode6 小时前
基于Matlab可见光通信系统中OOK调制的误码率性能建模与分析
算法·matlab·php