set-matrix-zeroes当元素为0则设矩阵内行与列均为0
题目描述
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) {
vector<bool> col,row;
col.resize(matrix[].size(), false);
row.resize(matrix.size(), false);
for (int i = ; i < matrix.size(); ++i)
{
for (int j = ; j < matrix[i].size();++j)
{
if(matrix[i][j]==)
{
col[j]=true;
row[i]=true;
}
}
}
for (int i = ; i < matrix.size(); ++i)
{
for (int j = ; j < matrix[i].size();++j)
{
if(col[j]||row[i])
{
matrix[i][j]=;
}
}
}
}
};
最优解法:
首先判断第一行和第一列是否有元素为0,而后利用第一行和第一列保存状态,最后根据开始的判断决定是否将第一行和第一列置0
//时间复杂度O(mn),空间复杂度O(1)
//利用第一行和第一列的空间做记录
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
const int row = matrix.size();
const int col = matrix[].size();
bool row_flg = false, col_flg = false; //判断第一行和第一列是否有零,防止被覆盖
for (int i = ; i < row; i++)
if ( == matrix[i][]) {
col_flg = true;
break;
}
for (int i = ; i < col; i++)
if ( == matrix[][i]) {
row_flg = true;
break;
}
//遍历矩阵,用第一行和第一列记录0的位置
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 (row_flg)
for (int i = ; i < col; i++)
matrix[][i] = ;
if (col_flg)
for (int i = ; i < row; i++)
matrix[i][] = ;
}
};
set-matrix-zeroes当元素为0则设矩阵内行与列均为0的更多相关文章
- leetcode 【 Set Matrix Zeroes 】python 实现
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- 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. 很挫的一个想 ...
- leetcode_question_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. Follow ...
- [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 ...
- 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. cli ...
- LeetCode Set Matrix Zeroes(技巧+逻辑)
题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...
- 【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)
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]题(Java):Set Matrix Zeroes(矩阵置0)
题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. ...
- 55. Set Matrix Zeroes
Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...
随机推荐
- Android之Android apk动态加载机制的研究(二):资源加载和activity生命周期管理
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/23387079 (来自singwhatiwanna的csdn博客) 前言 为了 ...
- linux批量改名
rename 's/abc/xyz/' * 把包含abc的文件名,替换为xyz
- java 生成zip文件并导出
总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...
- Asp.Net 管道事件注册/HttpApplication事件注册
一.HttpApplication简介 在HttpRuntime创建了HttpContext对象之后,HttpRuntime将随后创建一个用于处理请求的对象,这个对象的类型为HttpApplicati ...
- AS 注解处理器 APT Processor MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Centos7 Mysql 双机热备实现数据库高可用
mysql双主热备,也称主主互备,目的是mysql数据库高可用,只支持双机,原因是mysql的复制是一主多从,但一个从服务器只能有一个主服务器. 双机热备的条件是双机mysql版本必须一致. 服务器分 ...
- 以快板之名说Android 应用程序电源管理
当里个当,当里个当.Android开发UE(用户体验)为导向,首要任务便是省电量. 当里个当,当里个当.有一设备立足于墙边,这个设备唤固定电话.你的app造成这样,用户很快把你弃墙角.你咆哮耗电奈何与 ...
- 求通俗讲解下tensorflow的embedding_lookup接口的意思
https://www.zhihu.com/question/48107602 作者:王凯链接:https://www.zhihu.com/question/48107602/answer/15980 ...
- 转:UFLDL_Tutorial 笔记(deep learning绝佳的入门资料 )
http://blog.csdn.net/dinosoft/article/details/50103503 推荐一个deep learning绝佳的入门资料 * UFLDL(Unsupervised ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十八)ES6.2.2 增删改查基本操作
#文档元数据 一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息. 三个必须的元数据元素如下:## _index 文档在哪存放 ## _type 文档表示的对象类别 ## ...