[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 ...
随机推荐
- 从hbase到hive,以及sqoop转到mysql解析
https://blog.csdn.net/qq_33689414/article/details/80328665 hive关联hbase的配置文件 hive和hbase同步https://cwik ...
- jsPlumb学习笔记
这就是一个给元素画连接线的工具. <!DOCTYPE html> <html> <head> <title>jsPlumb</title> ...
- Android-Java-进程与线程
1.进程:什么是进程: Mac操作系统,Windows操作系统 ...... 等等,都是由多个进程来运行(系统进程,普通进程,等) 操作系统最小的控制单元是进程,一个应用就是一个进程 进程 全称为:操 ...
- 适合新手看的ref和out
面试的时候一般很高的概率会问到ref和out的区别...我们死记硬背的话很难记住. 建议大家和我一样简单的探索一下.动手试一下就能记住了. 共同点是我们在使用ref或者out的时候一定要在写的方法里面 ...
- windows下MySQL的安装(非安装包)
命令代码 "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqld.exe" --install MySQL56_3308 --def ...
- 主机安全扫描工具-- vuls
https://vuls.io/ 一. 安装 系统管理员有责任定期去检查系统的弱点和更新软件, vuls 可以提供如下功能: 通知管理员机器有安全隐患 支持本地和远程扫描(需要有 ssh 权限) 可以 ...
- RabbitMQ Java实例
引入RabbitMQ的jar包 <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amq ...
- jQuery核心函数的四种不同用法
核心函数根据实参的不同,有四种不同的用法. ①传一个函数作为参数 例如:$(function(){}) 作用:和window.onload = function(){}类似,它会在文档加载完成之后运行 ...
- 如何正确的加载和执行 JavaScript 代码
无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载和执行 ...
- aspectj
http://stackoverflow.com/questions/25209339/spring-aspectj-weaving-for-java-8-using-aspectj-maven-pl ...