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;
    }
}
相关推荐
张人玉1 小时前
C# 常量与变量
java·算法·c#
weixin_446122462 小时前
LinkedList剖析
算法
百年孤独_3 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表
我爱C编程3 小时前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
算法_小学生3 小时前
LeetCode 75. 颜色分类(荷兰国旗问题)
算法·leetcode·职场和发展
运器1233 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
算法_小学生3 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
岁忧3 小时前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
alphaTao3 小时前
LeetCode 每日一题 2025/6/30-2025/7/6
算法·leetcode·职场和发展
ゞ 正在缓冲99%…3 小时前
leetcode67.二进制求和
算法·leetcode·位运算