leetcode top100矩阵题73.54.48.240

73. 矩阵置零
复制代码
class Solution {
    public void setZeroes(int[][] matrix) {
        int line = matrix.length;
        int col = matrix[0].length;
        List<Integer> changeLines = new ArrayList<Integer>();
        List<Integer> changeCols = new ArrayList<Integer>();
        for(int i = 0; i < line; i++) {
            for(int j = 0; j < col; j++) {
                if (matrix[i][j] == 0) {
                    changeLines.add(i);
                    changeCols.add(j);
                }
            }
        }
​
        for(int i = 0; i < line; i++) {
            if (changeLines.contains(i)) {
                Arrays.fill(matrix[i], 0);
            }
            for(int j = 0; j < changeCols.size(); j++) {
                matrix[i][changeCols.get(j)] = 0;
            }
            
        }
        return;
    }
}
54. 螺旋矩阵
复制代码
class Solution {
    private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    public List<Integer> spiralOrder(int[][] matrix) {
        int line = matrix.length;
        int col = matrix[0].length;
        int size = line * col;
        List<Integer> ans = new ArrayList<>(line * col);
        int i = 0; 
        int j = -1;
        for(int di = 0; ans.size() < size; di = (di + 1) % 4) {
            for(int k = 0; k < col; k++) {
                i += DIRS[di][0];
                j += DIRS[di][1];
                ans.add(matrix[i][j]);
            }
            int tmp = col;
            col = line - 1;
            line = tmp;
        }
        return ans;
    }
}
48. 旋转图像
复制代码
class Solution {
    public void rotate(int[][] matrix) {
        int lines = matrix.length;
        int cols = matrix[0].length;
        int[][] copy = new int[lines][cols];
        for(int i = 0; i < lines; i++) {
            for(int j = 0; j < cols; j++) {
                copy[i][j] = matrix[i][j];
            }
        }
        for(int i = 0; i < lines; i++) {
            for(int j = 0; j < cols; j++) {
                matrix[j][cols - i - 1] = copy[i][j];
            }
        }
        return;
    }
}
240. 搜索二维矩阵Ⅱ
复制代码
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        for(int i = 0; i < m; i++) {
            if (matrix[i][n - 1] < target) {
                continue;
            } else if (matrix[i][n - 1] == target) {
                return true;
            }
            for(int j = n - 1; j >= 0; j--) {
                if (matrix[i][j] > target) {
                    continue;
                } else if (matrix[i][j] == target) {
                    return true;
                } else {
                    break;
                }
            }
        }
        return false;
    }
}
相关推荐
2501_941111241 小时前
C++与自动驾驶系统
开发语言·c++·算法
2501_941111692 小时前
C++中的枚举类高级用法
开发语言·c++·算法
jz_ddk2 小时前
[算法] 算法PK:LMS与RLS的对比研究
人工智能·神经网络·算法·信号处理·lms·rls·自适应滤波
Miraitowa_cheems2 小时前
LeetCode算法日记 - Day 106: 两个字符串的最小ASCII删除和
java·数据结构·算法·leetcode·深度优先
旭编2 小时前
小红的好矩形
c++·算法
小白程序员成长日记2 小时前
2025.11.12 力扣每日一题
算法·leetcode·职场和发展
Alex艾力的IT数字空间2 小时前
设计既保持高性能又兼顾可移植性的跨平台数据结构
数据结构·分布式·算法·微服务·中间件·架构·动态规划
leoufung2 小时前
贪心算法核心定理与应用——以 Gas Station 问题为例
算法·贪心算法
2501_941111463 小时前
C++与硬件交互编程
开发语言·c++·算法
未若君雅裁3 小时前
LeetCode 51 - N皇后问题 详解笔记
java·数据结构·笔记·算法·leetcode·剪枝