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;
    }
}
相关推荐
zander2589 分钟前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲25 分钟前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
祖力5543 分钟前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜1 小时前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
地平线开发者1 小时前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法
圣保罗的大教堂1 小时前
leetcode 3622. 判断整除性 简单
leetcode
_Narcissus_1 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
Lyyaoo.2 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
threerocks2 小时前
《The AI-Native SDLC Playbook》万字拆解
算法·aigc·ai编程
夏玉林的学习之路2 小时前
算法8.环形队列
算法