LeetCode73 Set Matrix Zeroes
题目:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.(Medium)
分析:
题意很简单,就是把为0的元素所在的行和列都置0;
首先不能直接边循环边做,这样会造成很多本来不是0的元素被置0之后,在后续判断中将它的行列也置0;
所以简单的方法是建立两个数组,存行和列是否为0的情况,遍历一遍更新两个数组,再遍历一遍把应该置0的全部置0。
自己写的时候先考虑用了个set便于不记录重复的下标,所以有如下代码1:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
set<int> rowflag;
set<int> colomnflag;
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
if (matrix[i][j] == ) {
rowflag.insert(i);
colomnflag.insert(j);
}
}
}
for (auto i : rowflag) {
for (int j = ; j < matrix[].size(); ++j) {
matrix[i][j] = ;
}
}
for (auto i : colomnflag) {
for (int j = ; j < matrix.size(); ++j) {
matrix[j][i] = ;
}
}
return;
}
};
程序还算简洁,记录最坏情况那里空间复杂度是为的O(nlogn + mlogm);
记录bool数组的方式,自己开始觉得写起来不够简洁。
后来看了讨论区大家的写法,发现第二次的用循环一遍,然后rowflag[i] || colonmnflag[j]可以很简洁的写出来,所以有如下代码2:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rowflag[matrix.size()] = {};
int colomnflag[matrix[].size()] = {};
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
if (matrix[i][j] == ) {
rowflag[i] = ;
colomnflag[j] = ;
}
}
}
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
if (rowflag[i] == || colomnflag[j] == ) {
matrix[i][j] = ;
}
}
}
return;
}
};
题目的follow-up中问道能否用常数的空间解决该问题,做法就是用两个变量记录第一行和第一列是否有0,
然后用第一行和第一列充当rowflag,colomnflag即可。
LeetCode73 Set Matrix Zeroes的更多相关文章
- Leetcode73. Set Matrix Zeroes矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输 ...
- 55. Set Matrix Zeroes
Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- Leetcode 细节实现 Set Matrix Zeroes
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- LeetCode: Set Matrix Zeroes 解题报告
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...
- 【LeetCode】73. Set Matrix Zeroes (2 solutions)
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- [Swift]LeetCode73. 矩阵置零 | Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
随机推荐
- 引入样式表(css)的四种方式
一.使用style属性: 将style属性直接加在html标签里. <标签 style="属性1: 设定值1; 属性2: 设定值2; "> 例如: <td sty ...
- python 读取xml文件
首先,获得标签信息abc.xml <?xml version="1.0" encoding="utf-8"?> <catalog> &l ...
- 1、jxl导入/导出excel案例,黏贴即可运行
package junit.test; import java.io.File; import java.io.IOException; import java.util.ArrayList; imp ...
- 集训队日常训练20180518-DIV1
A.3583 n根木棍是否能分成相等两堆. 背包dp,首先求和sum,如果为偶数就说明不行,否则考虑做一个sum/2大小的背包. #include<bits/stdc++.h> using ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]
[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...
- SQL Sever实验一 创建和删除数据库数据表
一. 实验目的 1. 熟悉SQL Server 2008 中SQL Server Management Studio的环境 2. 了解SQL Server ...
- JavaScript--location.href的跳转
页面重载 true 强制从服务器加载 false 优先从缓存加载 window.location.reload(true); window.location.href.self.location. ...
- Codeforces 113C
题目链接 C. Double Happiness time limit per test 5 seconds memory limit per test 128 megabytes input sta ...
- jstree设置checkbox单选
jstree设置插件checkbox只允许单选 jstree version console.log($.jstree.version); 3.3.8 单选配置参数: $.jstree.default ...
- 为什么DW的可视化下看到的效果与浏览器的效果有所区别?
可视区不是调用外面浏览器,Dreamweav 可视化区是为用户编辑而设计. 支持最基本的 HTML 与 CSS ,对 CSS 而言,我写入样式时如果你使用最基本的样式时它显示与你浏览器中看的效果相差不 ...