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 分钟前
C++ 线程间通信(二)
c++
minji...4 分钟前
Linux 高级IO(七)多进程、多线程的Reactor反应堆模式扩展、OTOL
linux·运维·c++·多路转接·epoll·reactor反应堆模型
晚风吹红霞5 分钟前
C++ list 容器完全指南:从入门到手撕双向链表
c++·链表·list
handler015 分钟前
【Linux 网络】:poll/epoll 底层机制与 Reactor 并发模型
linux·运维·服务器·网络·c++·多路转接·多路复用
cpp_25017 分钟前
P10109 [GESP202312 六级] 工作沟通
数据结构·c++·算法·题解·洛谷·gesp六级
Xeon_CC9 分钟前
vs2026远程开发debian12容器的C++程序笔记
开发语言·c++·笔记
ʚ希希ɞ ྀ10 分钟前
全排列 --- 回溯
算法·leetcode·深度优先
玉树临风ives11 分钟前
atcoder ABC 460 题解
数据结构·c++·算法
少司府13 分钟前
C++进阶:二叉搜索树
开发语言·数据结构·c++·二叉树·stl·二叉搜索树·tree
8Qi817 分钟前
LeetCode 124. 二叉树中的最大路径和(Hard)
算法·leetcode·二叉树·递归