[LeetCode]SetMatrix Zero
题目说明
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
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?
思路
此题要解决很简单,关键是空间限制。最笨的办法是另外开辟一个mn的矩阵记录每个元素是否应该变成0,最后遍历原矩阵将相应元素改成0.其次一点的办法是用两个hashset记录应该变为0的行和列。最后修改原矩阵。
这两种方法都很好实现,这里就不赘述了,要如何用常数量的空间解决这个问题呢?
如果直接修改矩阵元素的话该元素原来的信息就丢失了,因此思路是用两个布尔变量记录第一行和第一列是否有0,然后用第一行和第一列里的元素记录对应行列是否应该变为0。举例来说,第j列如果含0,就将对应的第一行matrix[0][j]设为0(这个元素最后必然要变成0,而他本身的值对第一行的影响已经记录了,因此这里修改他不会影响最后的结果)。第i行如果含0,就将第一列matrix[i][0]设为0。这样最后其余所有(n-1)行(m-1)列是否含0的信息都记录在对应第一行,第一列元素里。而第一行,第一列初始是否含0则记录在之前提到的两个布尔值了。
代码
if(matrix.length==0)
return;
int n=matrix.length;
int m=matrix[0].length;
boolean firstRow=false;
boolean firstCol=false;
for(int i=0;i<n;i++)
{
if(matrix[i][0]==0)
{
firstCol=true;
break;
}
}
for(int i=0;i<m;i++)
{
if(matrix[0][i]==0)
{
firstRow=true;
break;
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{
if(matrix[i][j]==0)
{
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(int i=1;i<n;i++)
{
if(matrix[i][0]==0)
{
for(int j=1;j<m;j++)
{
matrix[i][j]=0;
}
}
}
for(int i=1;i<m;i++)
{
if(matrix[0][i]==0)
{
for(int j=1;j<n;j++)
{
matrix[j][i]=0;
}
}
}
if(firstRow)
{
for(int i=0;i<m;i++)
matrix[0][i]=0;
}
if(firstCol)
{
for(int i=0;i<n;i++)
matrix[i][0]=0;
}
[LeetCode]SetMatrix Zero的更多相关文章
- 用JavaScript刷LeetCode的正确姿势
虽然很多人都觉得前端算法弱,但其实 JavaScript 也可以刷题啊!最近两个月断断续续刷完了 leetcode 前 200 的 middle + hard ,总结了一些刷题常用的模板代码.走过路过 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- hdu 5018
http://acm.hdu.edu.cn/showproblem.php?pid=5018 任意给你三个数,让你判断第三个数是否在以前两个数为开头组成的Fibonacci 数列中. 直接暴力 #in ...
- [Linux-vi] The simple set of vi command
Source : https://www.cs.colostate.edu/helpdocs/vi.html What is vi? The default editor that comes wit ...
- C++互斥器:Mutex
互斥器的功能是,使多个线程和谐工作.同一时间内,只能有一个线程得到互斥对象,并获得资源操作权限,那么如果同一时间其他线程也想去操作资源,此时就会因为Mutex未处于激发状态,而无奈的等待…这时候,线程 ...
- Thread in depth 3:Synchronization
Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...
- C++ - explicit和volatile/const的内容
第一眼见到explicit和volatile可能会一愣一愣的觉得可能是c11或者c14新加的标识符. 其实不是这样,volatile和const两个关键字在C语言的第二个版本KR C的时候就被加入了C ...
- Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性
在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...
- JS 中的数据类型转换
转成字符串 String 1. 使用 toString方法 这种方法可以将 number, boolean, object,array,function 转化为字符串,但是无法转换 null, und ...
- .Net框架搭建:SQL Server EF MVC简单三层框架
https://blog.csdn.net/pukuimin1226/article/details/52313656
- LOJ#3086. 「GXOI / GZOI2019」逼死强迫症(矩阵快速幂)
题面 传送门 题解 先考虑全都放\(1\times 2\)的方块的方案,设防\(i\)列的方案数为\(g_i\),容易推出\(g_i=g_{i-1}+g_{i-2}\),边界条件为\(g_0=g_1= ...
- Java并发编程之happens-before
happens-before是JMM最核心的概念,理解happens-before是理解JMM的关键. 一.JMM的设计 首先,让我们先分析一下JMM的设计意图.从JMM的设计者的角度,在设计JMM的 ...