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;
            }
        }
    }
};
相关推荐
文丑颜不良啊4 小时前
LeetCode3041~3060题目解析
java·leetcode
eggwyw4 小时前
C++ union 的一种妙用法
开发语言·c++
文丑颜不良啊5 小时前
LeetCode3101~3120题目解析
java·leetcode
ShineWinsu6 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
小七在进步7 小时前
string的实现(1)
c++·笔记·visual studio
敲上瘾9 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
牢姐与蒯10 小时前
cpp算法之哈希
c++·算法·哈希算法
lcj251111 小时前
[C++] C++11
开发语言·c++·笔记
玄昌盛不会编程13 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
文丑颜不良啊13 小时前
LeetCode3061~3080题目解析
java·leetcode