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;
            }
        }
    }
};
相关推荐
布莱克6051 小时前
链表详解:定义、作用、应用场景及与数组的区别(C/C++ 实战)
c语言·开发语言·c++·链表
鹿角片ljp2 小时前
LeetCode 141. 环形链表|从 HashSet 到快慢指针 O (1) 空间最优解
算法·leetcode·链表
执子星海2 小时前
Registry Win32 API详解
c++·windows·microsoft·visualstudio·微软
江屿风3 小时前
C++OJ题经验总结(竞赛)5
开发语言·c++
ZC跨境爬虫3 小时前
LeetCode 219. 存在重复元素 II(滑动窗口 + 哈希表详解)
算法·leetcode·散列表
zh路西法3 小时前
【玩转VLA具身智能机械臂】(三):视觉避障——从 RGB-D 点云到 MoveIt octomap 的完整落地
c++·ros2·gazebo·moveit2·ompl·octomap
Navigator_Z4 小时前
LeetCode //C - 1220. Count Vowels Permutation
c语言·算法·leetcode
2601_956121974 小时前
线性DP(入门)
c++·算法·动态规划
彷徨而立4 小时前
【C/C++】多线程读写普通 int 变量的一些问题
java·c语言·c++
feilieren4 小时前
leetcode - 389. 找不同
算法·leetcode