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;
            }
        }
    }
};
相关推荐
沫璃染墨1 小时前
从零入门计算机网络系列(三十一):Linux I/O 多路复用详解——select 工作原理与底层机制
linux·运维·服务器·网络·c++·网络协议·信息与通信
沫璃染墨2 小时前
从零入门计算机网络系列(三十二):Linux I/O 多路复用详解——poll 工作原理、TCP 实战与性能瓶颈
linux·运维·服务器·网络·c++·网络协议·网络安全
劲夫学编程2 小时前
leetcode: 字母异位词分组
算法·leetcode
星恒随风2 小时前
C++11 加餐(3)并发支持库——atomic 原子操作与内存序详解
开发语言·c++·笔记
wy_hhxx2 小时前
[Leetcode] 动态规划(打家劫舍、0-1背包、完全背包)
算法·leetcode·动态规划·完全背包·0-1背包
大袁同学2 小时前
【Linux网络编程】:破局孤岛通四海,纵贯五层渡风云
linux·网络·c++·后端
fqbqrr2 小时前
2612C++,C++20实战心得
c++·c++20
白小飞不会飞3 小时前
【AUTOSAR】 E2E Transformer:把 E2E 库接到 RTE 上
c++·嵌入式硬件·车载系统·transformer·autosar
Shaoshing3 小时前
LeetCode100 32.最长有效括号
java·数据结构·leetcode
小白的高手之路4 小时前
【LeetCode 热题 100】240. 搜索二维矩阵 II|矩阵、二分查找、Z 字形查找(Python)
python·算法·leetcode·矩阵·力扣