当前位置: 首页 > news >正文

Leecode Hot50

文章目录

  • 矩阵
    • Solution73. 矩阵置零
    • Solution54. 螺旋矩阵
    • Solution48. 旋转图像
    • Solution240. 搜索二维矩阵 II
  • 二叉树
    • 二叉树的四种遍历结果
    • Solution94. 二叉树的中序遍历
    • Solution104. 二叉树的最大深度
    • Solution226. 翻转二叉树
    • Solution101. 对称二叉树
    • Solution543. 二叉树的直径
    • Solution102. 二叉树的层序遍历
    • Solution108. 将有序数组转换为二叉搜索树
    • Solution98. 验证二叉搜索树
    • Solution230. 二叉搜索树中第 K 小的元素
    • Solution199. 二叉树的右视图
    • Solution114. 二叉树展开为链表
    • Solution105. 从前序与中序遍历序列构造二叉树
    • Solution437. 路径总和 III
    • Solution236. 二叉树的最近公共祖先

矩阵

Solution73. 矩阵置零

在这里插入图片描述

代码思路说明:
     假如矩阵是
                {1, 2, 3},
                {4, 0, 6},
                {7, 8, 9}
      第一次遍历矩阵,得到两个数组:
         zeroRows = [false, true, false],这个zeroRows记录需要置零的行,第二行需要置零
         zeroCols = [false, true, false],这个zeroCols记录需要置零的列,第二列需要置零
      第二次遍历矩阵,
         如果某个元素所在的行或列被标记为需要置零,则将该元素置为零;如元素4所在的行被标记为需要置零,则将该元素置为零
public class Solution {
   
    
    public static void setZeroes(int[][] matrix) {
   
        int m = matrix.length; // 矩阵的行数
        int n = matrix[0].length; // 矩阵的列数

        // 第一遍遍历,记录需要置零的行和列
        boolean[] zeroRows = new boolean[m]; // 记录需要置零的行
        boolean[] zeroCols = new boolean[n]; // 记录需要置零的列

        for (int i = 0; i < m; i++) {
   
            for (int j = 0; j < n; j++) {
   
                if (matrix[i][j] == 0) {
   
                    zeroRows[i] = true;
                    zeroCols[j] = true;
                }
            }
        }

        // 第二遍遍历,根据记录的结果将对应行和列置为零
        for (int i = 0; i < m; i++) {
   
            for (int j = 0; j < n; j++) {
   
                if (zeroRows[i] || zeroCols[j]) {
   
                    matrix[i][j] = 0;
                }
            }
        }
    }

    // 测试函数
    public static void main(String[] args) {
   
        int[][] matrix = {
   
                {
   1, 2, 3},
                {
   4, 0, 6},
                {
   7, 8, 9}
        };

        System.out.println("原始矩阵:");
        printMatrix(matrix);

        setZeroes(matrix);

        System.out.println("处理后的矩阵:");
        printMatrix(matrix);
    }

    /**
     * 打印矩阵
     *
     * @param matrix 待打印的矩阵
     */
    public static void printMatrix(int[][] matrix) {
   
        for (int[] row : matrix) {
   
            for (int num : row) {
   
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}

Solution54. 螺旋矩阵

这里是引用

 矩阵是:
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 9}
 ①从左到右遍历,遍历了1->3  用matrix[top][i]其中top是0,而i是0,1,2
 ②从从上到下遍历,遍历了6->9  用matrix[i][right]其中right是2,而i是1,2
 ③从右到左遍历,遍历了8->7  用matrix[bottom][i]其中bottom是2,而i是1,0
 ④从下到上遍历左边,遍历了7->4  用matrix[i][left]其中left是0,而i是2,1
 ⑤从左到右遍历,遍历了4->5  用matrix[top][i]其中top是1,而i是0,1
public class Solution {
   

    public List<Integer> spiralOrder(int[][] matrix) {
   
        List<Integer> result = new ArrayList<>();

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
   
            return result; // 如果矩阵为空,则直接返回空列表
        }

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

        while (left <= right && top <= bottom) {
   
            // 从左到右遍历上边,遍历了1->3和最后的4->5
            for (int i = left; i <= right; i++) {
   
                result.add(matrix[top][i]);
            }
            top++;

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

            // 如果还有行需要遍历(注意此时top已经加1,所以要判断top-1是否小于等于bottom)
            if (top <= bottom) {
   
                // 从右到左遍历下边,遍历了8->7
                for (int i = right; i >= left; i--) {
   
                    result.add(matrix[bottom][i]);
                }
                bottom--;
            }

            // 如果还有列需要遍历(注意此时right已经减1,所以要判断left是否小于等于right)
            if (left <= right) {
   
                // 从下到上遍历左边,遍历了7->4
                for (int i = bottom; i >= top; i--) {
   
                    result.add(matrix[i][left]);
                }
                left++;
            }
        }

        return result;
    }

    // 主函数,用于测试
    public static void main(String[] args) {
   
        Solution spiralMatrix = new Solution();

        int[][] matrix = {
   
                {
   1, 2, 3},
                {
   4, 5, 6},
                {
   7, 8, 9}
        };

        List<Integer> result = spiralMatrix.spiralOrder(matrix);

        // 打印结果
        for (int num : result) {
   
            System.out.print(num + " ");
        }
    }
}

Solution48. 旋转图像

这里是引用

第一步:转置矩阵
      将矩阵的行变成列,列变成行。在代码中,通过遍历矩阵的上三角(包括对角线),并交换 matrix[i][j] 和 matrix[j][i] 来实现转置。
      1, 2             1, 3
      3, 4   转置后变成  2, 4
 第二步:反转每一行
     遍历每一行,每一行数据都进行反转
      1, 3                  3, 1
      2, 4   每一行反转后变成  4, 2
 如此变完成了旋转90度
public class Solution {
   
    // 主方法,用于测试
    public static void main(String[] args) {
   
        int[][] matrix = {
   
                {
   1, 2, 3},
                {
   4, 5, 6},
                {
   7, 8, 9}
        };

        rotate(matrix);

        // 打印旋转后的矩阵
        for (int[] row : matrix) {
   
            for (int num : row) {
   
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }

    /**
     * 将二维矩阵原地顺时针旋转90度
     * @param matrix n × n 的二维矩阵
     */
    public static void rotate(int[][] matrix) {
   
        int n = matrix.length;

        // 第一步:转置矩阵
        for (int i = 0; i < n; i++) {
   
            for (int j = i; j < n; j++) {
   
                // 交换matrix[i][j]和matrix[j][i]
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

        // 第二步:反转每一行
        for (int i = 0; i < n; i++) {
   
            int left = 0, right = n - 1;
            while (left < right) {
   
                // 交换matrix[i][left]和matrix[i][right]
                int temp = matrix[i][left];
                matrix[i][left] = matrix[i][right];
                matrix[i][right] = temp;
                left++;
                right--;
            }
        }
    }
}

Solution240. 搜索二维矩阵 II

这里是引用

从矩阵的右上角或左下角开始搜索:
      从右上角开始搜索时,如果当前元素大于目标值,我们可以向左移动一列(因为左边的元素更小);
      如果当前元素小于目标值,我们可以向下移动一行(因为下边的元素更大)。
      这样,我们总是朝着可能包含目标值的方向移动,直到找到目标值或搜索到矩阵的边界为止。
public class Solution {
   
    // 主方法,用于测试
    public static void main(String[] args) {
   
        int[][] matrix = {
   
                {
   1, 4, 7, 11, 15},
                {
   2, 5, 8, 12, 19},
                {
   3, 6, 9, 16, 22},
                {
   10, 13, 14, 17, 24},
                {
   18, 21, 23, 26, 30}
        };
        int target = 5;

        boolean result = searchMatrix(matrix, target);
        System.out.println("目标值是否存在: " + result);
    }
    
    public static 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; // 矩阵的列数
        int row = 0; // 从第一行开始
        int col = n - 1; // 从最后一列开始

        while (row 

相关文章:

  • Zerotier虚拟局域网在树莓派的应用和Syncthing配合Zerotier实现端到端文件同步
  • Spring AOP面向切面编程实现日志功能
  • 第 五 章:优化算法_《C++性能优化指南》_notes
  • 在 CentOS 系统中开机自动执行 Shell 脚本
  • vue vue3 走马灯Carousel
  • 正则表达式-万能表达式
  • (二)手眼标定——概述+原理+常用方法汇总+代码实战(C++)
  • 第三章:测量性能_《C++性能优化指南》_notes
  • 可以把后端的api理解为一个目录地址,但并不准确
  • MQTT协议笔记
  • AI之山,鸿蒙之水,画一幅未来之家
  • 网络之数据链路层
  • 【赵渝强老师】Oracle数据库的客户端工具
  • python爬虫WASM
  • 大模型工作原理深度解剖:从Transformer架构到知识涌现的范式革命
  • 全息教学系统的软件开发,沉浸式数字沙盘展示系统如何改变历史教学
  • 蓝桥与力扣刷题(蓝桥 蓝桥骑士)
  • 腾讯滑块验证码自动分析工具:原理与实现
  • 《TypeScript 7天速成系列》第3天:TypeScript高级类型通关秘籍:泛型+联合+交叉类型实战
  • 【react】类组件和函数组件的区别
  • 中青报:“爸妈替我在线相亲”,助力还是越界?
  • 五一假期多地政府食堂对外开放:部分机关食堂饭菜“秒没”
  • 高速变道致连环车祸,白车“骑”隔离栏压住另一车,交警回应
  • 今年4月上海一二手房成交面积同比增21%,二手房成交2.07万套
  • 五大光伏龙头一季度亏损超80亿元,行业冬天难言结束
  • 200枚篆刻聚焦北京中轴线,“印记”申遗往事