【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 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?
解法一:
使用数组分别记录需要置零的行列。然后根据数组信息对相应行列置零。
空间复杂度O(m+n)
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty() || matrix[].empty())
return; int m = matrix.size();
int n = matrix[].size(); vector<bool> row(m, false);
vector<bool> col(n, false); for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(matrix[i][j] == )
{
row[i] = true;
col[j] = true;
}
}
} for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(row[i] == true)
matrix[i][j] = ;
if(col[j] == true)
matrix[i][j] = ;
}
}
}
};
解法二:
使用第一行和第一列记录该行和该列是否应该置零。
对于由此覆盖掉的原本信息,只要单独遍历第一行第一列判断是否需要置零即可。
空间复杂度O(1)
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty() || matrix[].empty())
return;
int m = matrix.size();
int n = matrix[].size();
bool col0 = false;
bool row0 = false;
for(int i = ; i < m; i ++)
{
if(matrix[i][] == )
{
col0 = true;
break;
}
}
for(int j = ; j < n; j ++)
{
if(matrix[][j] == )
{
row0 = true;
break;
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(matrix[i][j] == )
{
matrix[][j] = ;
matrix[i][] = ;
}
}
}
for(int i = ; i < m; i ++)
{
if(matrix[i][] == )
{
for(int j = ; j < n; j ++)
{
matrix[i][j] = ;
}
}
}
for(int j = ; j < n; j ++)
{
if(matrix[][j] == )
{
for(int i = ; i < m; i ++)
{
matrix[i][j] = ;
}
}
}
if(col0 == true)
{
for(int i = ; i < m; i ++)
{
matrix[i][] = ;
}
}
if(row0 == true)
{
for(int j = ; j < n; j ++)
{
matrix[][j] = ;
}
}
}
};
【LeetCode】73. Set Matrix Zeroes (2 solutions)的更多相关文章
- 【LeetCode】-- 73. Set Matrix Zeroes
问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...
- 【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 ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 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】867 - Transpose Matrix
[题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
随机推荐
- 在pcDuino上使用蓝牙耳机玩转音乐
1.资源 pcDuino板子一个.HDMI to VGA线一条.电源线一条.USB hub一个.显示器.鼠标.键盘.蓝牙适配器.蓝牙耳机. 2.资源已经到位,让我们开始吧 1.在ubuntu上安装蓝牙 ...
- 解决Visual Studio 2015创建工程时的“DNX SDK version 'dnx-clr-win-x86.1.0.0-beta5' failed to install.”错误
前段时间发布了Visual Studio2015,在后,发现创建Asp.Net工程和时,出现了"DNX SDK version 'dnx-clr-win-x86.1.0.0-beta5' f ...
- [转].net reactor 学习系列(二)---.net reactor界面各功能说明
安装了.net reactor之后,可以在安装目录下找到帮助文档REACTOR_HELP.chm,目前没有中文版本,里面详细介绍了.net reactor的各功能及使用场景.本系列文章是基于此帮助文档 ...
- 【转】2012年6月25 – 某欧美上市企业PHP工程师最新面试题
笔试: 尼玛,连页眉页脚都是英文!不过都还能读懂.题目很简单.印象深刻的有几题. 具体题目忘了,主要知识点考点是,建立的视图,实现有自增字段. 答:之前还真没考虑过这个问题.当时条件发射,给了一个用户 ...
- Windows Server 2008 Standard Enterprise Datacenter各个版本区别
-- Windows Server 2008 Standard 包含1个虚拟实例许可,5个客户端访问授权,售价999美元. -- Windows Server 2008 Enterprise 包含4个 ...
- B+树索引
结构上 B树中关键字集合分布在整棵树中,叶节点中不包含任何关键字信息,而B+树关键字集合分布在叶子结点中,非叶节点只是叶子结点中关键字的索引: B树中任何一个关键字只出现在一个结点中,而B+树中的关键 ...
- springboot 选择启动某个配置文件
选择启动某个配置文件 Spring Boot配置文件提供了隔离一部分应用程序配置的方法,并可使其仅在某指定环境可用.任何有@Component和@Configuration注解的Bean都用@prof ...
- Java 模拟新浪登录 2016
想学习一下网络爬虫.涉及到模拟登录,查阅了一番资料以后发现大部分都有点过时了,就使用前辈们给的经验,Firefox抓包调试,採用httpclient模拟了一下新浪登录. 不正确之处多多包括.须要的能够 ...
- iOS: 沙盒的详解和目录的获取
沙盒的详解: •iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被称为沙盒 •iOS常用目录: –Bundle //该目录下的文件是用来存储应用程序包的 ...
- 数学图形(2.2)N叶结
上一节讲的三叶结,举一反三,由三可到无穷,这一节讲N叶结 再次看下三叶结的公式: x = sin(t) + 2*sin(2*t)y = cos(t) - 2*cos(2*t) 将其改为: x = si ...