矩阵【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;
    }
相关推荐
杨云强12 小时前
离散积分,相同表达式数组和公式
算法
地平线开发者12 小时前
征程 6 | BPU trace 简介与实操
算法·自动驾驶
满天星830357712 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
怪兽201412 小时前
SQL优化手段有哪些
java·数据库·面试
ss27312 小时前
手写MyBatis第107弹:@MapperScan原理与SqlSessionTemplate线程安全机制
java·开发语言·后端·mybatis
Lris-KK13 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
Deschen13 小时前
设计模式-原型模式
java·设计模式·原型模式
麦麦鸡腿堡13 小时前
Java的动态绑定机制(重要)
java·开发语言·算法
それども13 小时前
SpringBootTest运行线程池被拒绝
java
zy_destiny13 小时前
【工业场景】用YOLOv8实现抽烟识别
人工智能·python·算法·yolo·机器学习·计算机视觉·目标跟踪