class Solution { public: void setZeroes(vector<vector<int> > &matrix) { int h = matrix.size(); int w = 0; if (h > 0) w = matrix[0].size(); bool c1 = false, r1 = false; for (int i = 0; i < h; i++) { if (matrix[i][0] == 0) {c1 = true; break;} } for (int i = 0; i < w; i++) { if (matrix[0][i] == 0) {r1 = true; break;} } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++){ if (matrix[i][j] == 0) { matrix[0][j] = matrix[i][0] = 0; } } } for (int i = 1; i < h; i++) { for (int j = 1; j < w; j++) { if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; } } if (c1) for (int i = 0; i < h; i++) matrix[i][0] = 0; if (r1) for (int j = 0; j < w; j++) matrix[0][j] = 0; } };
相关推荐
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
leetcode中文版
vs code LeetCode 插件
大佬的leetcode刷题笔记(c++版本)
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
Set Matrix Zeroes 第一种 通过一次遍历记录所有为0的索引(Python中enumerate()输出当前列表的索引) 再遍历一次, 根据记录的索引进行置0 第二种 通过一次遍历所有为0的索引, 设置当前索引的行列的第一个数为0, 作为...
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
leetcode高频面试笔试题150+道,亲身总结。
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
LeetCode面试笔试题
LeetCode 刷题笔记
LeetCode 刷题
(C++)LeetCode刷题题解答案
leetcode上的题目,网站上测试通过,可以借鉴下