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获取屏幕长宽的方法
package com.kale.imageview02; import android.annotation.SuppressLint; import android.app.Activity; i ...
- MVC异步分页
如图: 1: 控制器代码 // // GET: /AjaxUser/ shopEntities shop = new shopEntities(); public ActionResult Index ...
- Can't create pdf file with font calibri bold 错误解决方案
错误情况: %%[ ProductName: Distiller ]%% Mangal not found, using Courier. %%[ Error: invalidfont; Offend ...
- Qt中printsupport的注意点和使用方法
问题:Qt中包含QPrintDialog.QPrinter.QPrintPreviewDialog失败:在引入printsupport后报cpp:651: error: undefined refer ...
- IIS(互联网信息服务)
ylbtech-Miscellaneos:IIS(互联网信息服务) IIS是Internet Information Services的缩写,意为互联网信息服务,是由微软公司提供的基于运行Micros ...
- Redhat Linux NFS配置
Linux下,All deviceis file,所有的设备都是文件.当我们需要把某些文件夹就或者文件共享给其他用户,就可以使用网络文件系统. 本文介绍Redhat Linux下的NFS配置. 在使用 ...
- Qt信号槽的一些事 Qt::带返回值的信号发射方式
一般来说,我们发出信号使用emit这个关键字来操作,但是会发现,emit并不算一个调用,所以它没有返回值.那么如果我们发出这个信号想获取一个返回值怎么办呢? 两个办法:1.通过出参形式返回,引用或者指 ...
- lazarus汉化
启动Lazarus IDE,点击菜单栏中的Environment,再点击Options选项 在弹出的IDE选项框内,点选左侧Environment下的Desktop子选项,将Language设为Chi ...
- Android -- EventBus使用
EventBus EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间.组件与后台线程间的通信.比如请求网络,等网络返回时通过Handler ...
- 理解TensorFlow的Queue
https://www.jianshu.com/p/d063804fb272 这篇文章来说说TensorFlow里与Queue有关的概念和用法. 其实概念只有三个: Queue是TF队列和缓存机制的实 ...