[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 ...
随机推荐
- linux 通过md5查找重复文件
代码如下: md5sum *|sort |uniq -w32 -D|awk -F ' ' '{print $2}' uniq 部分参数 -c #在每行前显示该行重复次数. -d #只输出重复的行. - ...
- underscore objects
1._.keys():获取对象的属性名,不包含原型链 _.keys = nativeKeys || function(obj) { if (obj !== Object(obj)) throw new ...
- 通过FactoryBean方式来配置bean
1.实现FactoryBean接口的java类: 2.相应的.xml文件:
- Dwz(J-UI)框架--入门
http://www.cnblogs.com/chenyongsai/p/4933982.html Dwz(J-UI)框架--入门 一.了解 概述:是中国人自己开发的基于jQuery实现的Ajax R ...
- ASP.NET WEB API 返回JSON 出现2个双引号问题
前言 在使用ASP.NET WEB API时,我想在某个方法返回JSON格式的数据,于是首先想到的就是手动构建JSON字符串,如:"{\"result\" ...
- 使用base64转码的方式上传图片
1.前端html代码 <input style="width:100%" onchange="loadpicture(1)" type="fil ...
- C#不用union,而是有更好的方式实现
用过C/C++的人都知道有个union,特别好用,似乎char数组到short,int,float等的转换无所不能,也确实是能,并且用起来十分方便.那C#为什么没有这个关键字呢?怎么实现这个功能?其实 ...
- 几条shell命令
windows: route add 158.0.0.0 mask 255.0.0.0 158.137.38.1 metric 3 linux: netstat -apn 查看所有端口使用,可查看端 ...
- Android 创建自定义 View 的属性 (attrs) 时需要注意的问题
自定义 View 的属性并不难,可以参照官方的文档 https://developer.android.com/training/custom-views/create-view.html 但是需要注 ...
- Day 49 CSS样式.
外部样式 1.元素选择器 /*1. 元素选择器*/p{ color :red} <p>1.我是一个p标签</p> 2.ID选择器 /*2 ID 选择器*/#p{ color : ...