73. Set Matrix Zeroes (Array)
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(mn) 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?
思路:第一行和第一列用来标示该行、该列是否全0,但事先得判断第一行、第一列是否全0=>用两个额外的变量存储
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty()) return;
bool firstLineZero = false;
bool firstColumnZero = false;
if(matrix[][]==){
firstLineZero = true;
firstColumnZero = true;
}
//the first line
for(int i = ; i<matrix[].size(); i++)
{
if(matrix[][i]!=) continue;
firstLineZero = true;
break;
}
//the first column
for(int i = ; i<matrix.size(); i++)
{
if(matrix[i][]!=) continue;
firstColumnZero = true;
break;
}
for(int i = ; i < matrix.size(); i++)
{
for(int j = ; j<matrix[].size(); j++)
{
if(matrix[i][j] != ) continue;
matrix[i][] = ;
matrix[][j] = ;
}
}
for(int i = ; i<matrix[].size(); i++)
{
if(matrix[][i]!=) continue;
for(int j = ; j<matrix.size(); j++)
{
matrix[j][i]=;
}
}
for(int i = ; i<matrix.size(); i++)
{
if(matrix[i][]!=) continue;
for(int j = ; j<matrix[].size(); j++)
{
matrix[i][j]=;
}
}
if(firstLineZero)
{
for(int i = ; i< matrix[].size(); i++)
{
matrix[][i] = ;
}
}
if(firstColumnZero)
{
for(int i = ; i< matrix.size(); i++)
{
matrix[i][] = ;
}
}
}
};
73. Set Matrix Zeroes (Array)的更多相关文章
- 【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 ...
- 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. Fo ...
- Leetcode#73 Set Matrix Zeroes
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
- [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. Follow ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- LeetCode OJ 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. 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. Fo ...
- 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. 重点是空间复 ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- tintColor 与 UIImage.renderingMode 渲染
在iOS7中,UIView新增了一个属性tintColor.这是一个UIColor,被使用在UIView中改变应用程序的外观的.默认tintColor的值为nil,这表示它将会运用父视图层次的颜色来进 ...
- L205 EE
As an Electrical Engineer at P, you will: Design, build, and verify PCBAsCreate, modify, and update ...
- Winform开发之SqlCommand常用属性和方法
SqlCommand类表示要对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程,有若干个属性和若干个方法,具体的各类方法使用可以从msdn上找到. 这里介绍几个常用东 ...
- iOS-----使用AddressBook添加联系人
使用AddressBook添加联系人 添加联系人的步骤如下: 1 创建ABAddressBookRef,这就得到了对地址簿的引用. 2 调用ABPersonCreate()函数创建一个空的ABReco ...
- java MessageFormat.format 用法
FormatElement: { ArgumentIndex }:是从0开始的入参位置索引. { ArgumentIndex , FormatType } { ArgumentIndex , Form ...
- BZOJ2118: 墨墨的等式(同余类BFS)(数学转为图论题)
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2944 Solved: 1206[Submit][Status][Discu ...
- C-语言第二次作业(大一下)
要求一.设计过程 作业(1) 1.提交列表 6-7 删除字符串中数字字符 2.设计思路(6分 ...
- 《DSP using MATLAB》示例 Example 9.8
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 【java基础】java关键字final
谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法.下 ...
- 【java】JDK环境变量配置
1.双击下载的JDK 设置安装路径.这里我们选择默认安装在C:\Program Files\Java\jdk1.8.0_51(java 环境分JDK 和JRE ,JDK就是Java Developm ...