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. 思路:不能用 ...
随机推荐
- 使用session实现一次性验证码
在登录页面和各种页面,会看到有验证码输入,这样做的目的是为了防止密码猜测工具破解密码,保护了用户密码安全,验证码只能使用一次,这样就给密码猜测工具带来了很大的困难,基本上阻断了密码猜测工具的使用. 可 ...
- Vue Router 相关
1. 路由传参: 编程式的导航 router.push this.$router.push("home"); this.$router.push({ name: 'news', p ...
- WCF 服务
1.代码 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Seriali ...
- QT_强杀进程
#ifdef WIN32 bool res = false; HANDLE hToolHelp32Snapshot; hToolHelp32Snapshot = CreateToolhelp32Sna ...
- 常用的iOS 宏定义 (持续更新中)
1.System Versioning Preprocessor Macros #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevic ...
- python KMP算法介绍
- 通过DataWorks数据集成归档日志服务数据至MaxCompute进行离线分析
通过DataWorks归档日志服务数据至MaxCompute 官方指导文档:https://help.aliyun.com/document_detail/68322.html但是会遇到大家在分区上或 ...
- 全球首个百万IOPS云盘即将商业化 阿里云推出超高性能云盘ESSD
近日,在经过近半年的上线公测后,阿里云全球首个跨入IOPS百万时代的云盘——ESSD即将迎来商业化,单盘IOPS高达100万,这是阿里云迄今为止性能最强的企业级块存储服务. 搭配ECS云服务器使用, ...
- SPARK-SQL内置函数之字符串函数
转载请注明转自:http://www.cnblogs.com/feiyumo/p/8763186.html 1.concat对于字符串进行拼接 concat(str1, str2, ..., strN ...
- 未能加载文件或程序集 XXX 或它的一个依赖项。参数错误
引发原因 :电脑突然蓝屏重启 解决方法:删除 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files 下的所有文件 ...