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, ...
随机推荐
- 简析Window、Activity、DecorView以及ViewRoot之间的错综关系
一.职能简介 Activity Activity并不负责视图控制,它只是控制生命周期和处理事件.真正控制视图的是Window.一个Activity包含了一个Window,Window才是真正代表一个窗 ...
- Java并发编程的艺术(四)——线程的状态
线程的状态 初始态:NEW 创建一个Thread对象,但还未调用start()启动线程时,线程处于初始态. 运行态:RUNNABLE 在Java中,运行态包括就绪态 和 运行态. 就绪态 该状态下的线 ...
- AppServ与IIS快速共存搭建PHP环境
一:AppServ 一路安装 其中,不能与IIS端口冲突,比如可以指定端口为8080: 安装完毕后验证 http://localhost:8080,验证 MySql是否能够打开: 二:IIS整合 新建 ...
- cannot be resolved. It is indirectly referenced from required .class files
缺少引用. 把缺少的引用在导入一下...如果是mavan 在当前moudle里也要把 dependency加进来
- BZOJ 1093 最大半连通子图 题解
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2767 Solved: 1095[Submit][S ...
- [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V
Hello everyone, this is the third post of the series. . Background =============== In my solution, ...
- JQuery Ajax 在asp.net中使用总结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- Set Matrix Zeroes leetcode java
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- C# 6.0 的那些事
这两天期中考试没时间去看Connect();直播,挺可惜的,考完后补看了Connect(); 把C#6.0的新东西总结一下. 自动属性初始化 (Initializers for auto-proper ...
- 使用JDBC向Kudu表插入中文数据乱码(转载)
参考:https://cloud.tencent.com/developer/article/1077763 问题描述 使用Impala JDBC向Kudu表中插入中文字符,插入的中文字符串乱码,中文 ...