[Leetcode] 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.
Did you use extra space?
A straight forward solution using O(m n) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix)
{
int row=matrix.size();
int col=matrix[].size();
if(row==||col==) return;
bool rFlag=false,cFlag=false;
for(int i=;i<row;++i)
{
if(matrix[i][]==)
{
rFlag=true;
break;
}
}
for(int i=;i<col;++i)
{
if(matrix[][i]==)
{
cFlag=true;
break;
}
}
for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(matrix[i][j]==)
{
matrix[i][]=;
matrix[][j]=;
}
}
}
for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(matrix[i][]==||matrix[][j]==)
matrix[i][j]=;
}
}
if(rFlag)
{
for(int i=;i<row;++i)
matrix[i][]=;
}
if(cFlag)
{
for(int i=;i<col;++i)
matrix[][i]=;
}
}
};
代码参考了Gradyang的博客
[Leetcode] set matrix zeroes 矩阵置零的更多相关文章
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- [LeetCode] 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. click ...
- Leetcode73. Set Matrix Zeroes矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输 ...
- [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 ...
- 【python】Leetcode每日一题-矩阵置零
[python]Leetcode每日一题-矩阵置零 [题目描述] 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 .请使用 原地 算法. 进阶: 一个直观的解 ...
- leetcode刷题-73矩阵置零
题目 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1]]输出: ...
- [LeetCode] 73. 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:矩阵置零【73】
LeetCode:矩阵置零[73] 题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], ...
- Java实现 LeetCode 73 矩阵置零
73. 矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ...
随机推荐
- python 连接MSSQL
# -*- coding: utf-8 -*- import pymssql conn=pymssql.connect(host=".",user="sa",p ...
- Spark知识点
1.Spark架构 分布式spark应用中的组件 在分布式环境下,Spark集群采用的是主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器 ...
- 网站安全检测 漏洞检测 对thinkphp通杀漏洞利用与修复建议
thinkphp在国内来说,很多站长以及平台都在使用这套开源的系统来建站,为什么会这么深受大家的喜欢,第一开源,便捷,高效,生成静态化html,第二框架性的易于开发php架构,很多第三方的插件以及第三 ...
- 002---Python基本数据类型--字符串
字符串 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1p ...
- Python的文件输入输出,如何追加内容,读取内容,添加内容
python主要的文件打开的几种访问模式 模式可以为读模式('r').写模式('w')或追加模式('a'),当然还有rb.wb.ab.r+.w+.a+.rb+.wb+.ab+,然而实际从代码上我也没看 ...
- Redis进阶:数据持久化,安全,在PHP中使用
一.redis数据持久化 由于redis是一个内存数据库,如果系统遇到致命问题需要关机或重启,内存中的数据就会丢失,这是生产环境所不能允许的.所以redis提供了数据持久化的能力. redis提供了两 ...
- 【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}
这条题目当时卡了我们半天,于是成功打铁--今天回来一看,mmp,贪心思想怎么这么弱智.....(怪不得场上那么多人A了 题意分析 这里是原题: Tree Time Limit: 2000/1000 M ...
- webpack使用时可能出现的问题
1.在配置完webpack.config.js准备进行热加载开发时,修改React内容浏览器不会自动局部刷新,而且会console出一些提示: The following modules couldn ...
- ASP NET Core ---REST & HTTP GET
参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/h0764n405ll.html 一.REST (Representational Sta ...
- dell raid配置
常用查看命令:待有dell裸机环境会详细列出 megacli -LDInfo -Lall -aALL 查raid级别 megacli -AdpAllInfo -aALL 查raid卡信息 megacl ...