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. 思路:不能用 ...
随机推荐
- TomCat 启动默认加载项目
在最后加上这句代码即可:<Context path="" docBase="\项目名称" reloadable="true" cros ...
- Amazon EBS的功能更新
Amazon EBS(Elastic Block Store.简称EBS) 为 Amazon EC2 实例提供块级存储服务.EBS 卷须要通过网络訪问,而且能独立于实例的生命周期而存在.也就是说假如E ...
- 洛谷P1970 [NOIP2013提高组Day2T2] 花匠
P1970 花匠 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希 望剩下的花排 ...
- Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8, 1 ...
- R语言可视化--颜色
RColorBrewer包 三类调色板:sequential / diverging / qualitative 调色板的信息可以与colorRamp / colorRampPalette结合使用 从 ...
- 2018.8.6 模拟赛 提高组B
T1 Description 给定一个n个点m条边的有向图,有k个标记点,要求从规定的起点按任意顺序经过所有标记点到达规定的终点,问最短的距离是多少. Input 第一行5个整数n.m.k.s.t,表 ...
- .Net Core 授权系统组件解析
前面关于.Net Core如何进行用户认证的核心流程介绍完毕之后,.Net Core 认证系统之Cookie认证源码解析远程认证暂时不介绍,后期有时间,我会加上.接下去介绍认证组件是如何和认证组件一起 ...
- CEF 框架使用集锦
CEF 框架使用集锦: 参考:〓https://github.com/NetDimension/NanUI/wiki/%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8NanUI ...
- tes..
力扣刷题 二分查找法 二分查找法又称折半查找法. 优点:比较次数少,查找速度快,平均性能好: 缺点:要求待查表为有序表,且插入删除困难. 因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 首先 ...
- IntelliJ IDEA添加过滤文件或目录(转)
在idea上使用svn后,发现即使svn窗口添加过滤正则没有忽略.iml文件的提交,安装ignore插件后没发现有svn的忽略选项,最后发现这样设置就可以了: 1.Settings→Editor→Fi ...