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++·散列表·哈希
t***5447 小时前
如何配置Orwell Dev-C++使用Clang
开发语言·c++
CoderCodingNo7 小时前
【信奥业余科普】C++ 的奇妙之旅 | 13:为什么 0.1+0.2≠0.3?——解密“爆int”溢出与浮点数精度的底层原理
开发语言·c++
小辉同志8 小时前
215. 数组中的第K个最大元素
数据结构·算法·leetcode··快速选择
memcpy09 小时前
LeetCode 2615. 等值距离和【相同元素分组+前缀和;考虑距离和的增量】中等
算法·leetcode·职场和发展
极客智造9 小时前
深入详解 C++ 智能指针:RAII 原理、分类特性、底层机制与工程实战
c++·智能指针
alphaTao10 小时前
LeetCode 每日一题 2026/4/20-2026/4/26
算法·leetcode·职场和发展
王璐WL10 小时前
【C++】类的默认成员函数(上)
c++
王老师青少年编程11 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【区间贪心】:区间覆盖(加强版)
c++·算法·贪心·csp·信奥赛·区间贪心·区间覆盖(加强版)
宏笋11 小时前
C++11完美转发的作用和用法
c++