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;
            }
        }
    }
};
相关推荐
mjhcsp12 小时前
P14992 取模(题解)
c++
sin_hielo13 小时前
leetcode 3510
数据结构·算法·leetcode
老歌老听老掉牙13 小时前
16宫格属性分析系统:打造专业级科学数据可视化工具
c++·qt·可视化
橘子师兄13 小时前
C++AI大模型接入SDK—API接入大模型思路
开发语言·数据结构·c++·人工智能
CSDN_RTKLIB13 小时前
【字符编码】拷贝的是字符还是字节序列
c++
历程里程碑13 小时前
哈希3 : 最长连续序列
java·数据结构·c++·python·算法·leetcode·tornado
闻缺陷则喜何志丹13 小时前
【图论】P9661 [ICPC 2021 Macao R] Sandpile on Clique|普及+
c++·算法·图论·洛谷
2401_8414956413 小时前
【LeetCode刷题】两两交换链表中的节点
数据结构·python·算法·leetcode·链表·指针·迭代法
无尽的罚坐人生14 小时前
hot 100 560.和为 K 的子数组
数据结构·算法·leetcode
Ll130452529814 小时前
leetcode代码随想录数组篇
数据结构·算法·leetcode