C++ | Leetcode C++题解之第73题矩阵置零

题目:

题解:

cpp 复制代码
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        int flag_col0 = false;
        for (int i = 0; i < m; i++) {
            if (!matrix[i][0]) {
                flag_col0 = true;
            }
            for (int j = 1; j < n; j++) {
                if (!matrix[i][j]) {
                    matrix[i][0] = matrix[0][j] = 0;
                }
            }
        }
        for (int i = m - 1; i >= 0; i--) {
            for (int j = 1; j < n; j++) {
                if (!matrix[i][0] || !matrix[0][j]) {
                    matrix[i][j] = 0;
                }
            }
            if (flag_col0) {
                matrix[i][0] = 0;
            }
        }
    }
};
相关推荐
yolo_guo2 小时前
redis++使用: hmset 与 hmget
c++·redis
handler012 小时前
拒绝权限报错!三分钟掌握 Linux 权限管理
linux·c语言·c++·笔记·学习
阿Y加油吧3 小时前
算法实战笔记:LeetCode 169 多数元素 & 75 颜色分类
笔记·算法·leetcode
t***5443 小时前
如何在Dev-C++中选择Clang编译器
开发语言·c++
不要秃头的小孩3 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
汉克老师4 小时前
GESP2023年9月认证C++三级( 第一部分选择题(9-15))
c++·gesp三级·gesp3级
We་ct4 小时前
LeetCode 120. 三角形最小路径和:动态规划详解
前端·javascript·算法·leetcode·typescript·动态规划
py有趣4 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
py有趣6 小时前
力扣热门100题之编辑距离
数据结构·算法·leetcode
Wave8457 小时前
C++继承详解
开发语言·c++·算法