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;
            }
        }
    }
};
相关推荐
啊嘞嘞?几秒前
力扣(LRU缓存)
算法·leetcode
啊嘞嘞?3 分钟前
力扣(下一个排列)
算法·leetcode
程与留11 分钟前
09_Qt 布局管理系统——QHBoxLayout、QVBoxLayout、QGridLayout
c++·qt
神仙别闹14 分钟前
基于QT(C++)实现旅游线路规划系统
c++·qt·旅游
郝学胜_神的一滴28 分钟前
C++11 工程级应用 02:decltype与返回类型后置,让泛型代码告别类型玄学
c++·编程语言
tudousisi2221 小时前
8.25第二题训练
c++
星星.7221 小时前
2026河南萌新联赛第六场(郑州大学)补题B、D、L、J、I
数据结构·c++·算法
Forever Nore1 小时前
LeetCode 16 最接近的三数之和 - 双指针逼近
算法·leetcode·职场和发展
smj2302_796826521 小时前
解决leetcode第4033题有效K个不同元素子数组I
数据结构·python·算法·leetcode
witton1 小时前
xmake中将proto文件生成C/C++文件然后再编译链接进项目
c语言·c++·mingw·protobuf·xmake·protobuf-c·protoc-gen-c