LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
题目: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?
这个问题使用额外的空间来完成比较直观,但是人要求用“CONSTANT SPACE” 。。。
我想了半天也没想出来为啥,只有看看leetcode的讨论了。http://discuss.leetcode.com/questions/249/set-matrix-zeroes
其实也不是很难,稍微有点hack。就是首先用两个bool变量记录下第一行和第一列里面是不是有0。接下来利用第一行和第一列保存那些位于[1~m,1~n]范围内的元素的0的位置。就是,如果(i,j)是0,那么我们设置(i,0)和(0,j)为0. 所以其实第一行和第一列有点纵横坐标的意思,这样表示待会儿我们要把第i行和第j列都设置成0。

然后再次遍历[1~m,1~n],如果(i,0)或者(0,j)是0,那么(i,j)为0。注意到这次遍历的时候,那些本身在0行或者0列的0(图中的黄色)也造成了所在列或者行的设置。

最后,根据前面两个bool变量的值,设置第一行和第一列。注意到这个时候我们只设置行或者列,因为如果某行之前有0,所在的列在上一步中已经设置过了。

代码如下:
public void setZeroes(int[][] matrix) {
int rownum = matrix.length;
if (rownum == ) return;
int colnum = matrix[].length;
if (colnum == ) return;
boolean hasZeroFirstRow = false, hasZeroFirstColumn = false;
// Does first row have zero?
for (int j = ; j < colnum; ++j) {
if (matrix[][j] == ) {
hasZeroFirstRow = true;
break;
}
}
// Does first column have zero?
for (int i = ; i < rownum; ++i) {
if (matrix[i][] == ) {
hasZeroFirstColumn = true;
break;
}
}
// find zeroes and store the info in first row and column
for (int i = ; i < matrix.length; ++i) {
for (int j = ; j < matrix[].length; ++j) {
if (matrix[i][j] == ) {
matrix[i][] = ;
matrix[][j] = ;
}
}
}
// set zeroes except the first row and column
for (int i = ; i < matrix.length; ++i) {
for (int j = ; j < matrix[].length; ++j) {
if (matrix[i][] == || matrix[][j] == ) matrix[i][j] = ;
}
}
// set zeroes for first row and column if needed
if (hasZeroFirstRow) {
for (int j = ; j < colnum; ++j) {
matrix[][j] = ;
}
}
if (hasZeroFirstColumn) {
for (int i = ; i < rownum; ++i) {
matrix[i][] = ;
}
}
}
总结下。
这个方法并不是太容易想到。是个算比较hack的方法。当然也算一个技巧了。
LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]的更多相关文章
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
随机推荐
- 蜜果私塾:informix数据库学习合集[不断补充]
一.infomix使用备忘录 目录结构: 1. 启动与停止命令: 2. 修改数据库编码: 3. 查看informix占用的端口: 4. 使用dbacces ...
- CodeMirror与jquery UI-Tabs混合使用 注意事项
第一步:.将代码高亮渲染 第二步:jquery Tab输出: 第三步:点击Tab切换时,将代码块刷新: 参考:http://jtmorris.net/2013/06/codemirror-editor ...
- Spring Cloud(四):熔断器Hystrix
熔断器 雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者”的不可用导致“服务 ...
- 使用c++的cocos2d-x-3.0rc1程序公布apk
(如今cocos2dx-x-3.0正式版已经出了.之前用的cocos2d-x-3.0rc1,就先用这个版本号吧) 0. 完毕C++项目 在cmd下使用cocos.py new命令,然后习惯性的在win ...
- linux 流量控制全攻略(TC)
TC很是强大啊,很多所谓的硬件路由器,都是基于这个做的. TC介绍 在linux中,TC有二种控制方法CBQ和HTB.HTB是设计用来替换CBQ的.它是一个层次式的过滤框架.TC包括三个基本的构成块: ...
- SAP ECC6安装系列四:安装过程详解
原作者博客 http://www.cnblogs.com/Michael_z/ ======================================== 续接上篇,我们终于按下了 “Next” ...
- 【持续更新】GDB使用笔记
多文件程序的调试,例子: 文件结构: /demo Makefile /src demo.cpp util.cpp /include util.h 截图: ----------------------- ...
- Unity3D中UGUI使用ScrollBar之后保存,工程启动报错原因跟踪
笔者不久前将Unity3D版本更新为4.6.2之后.在使用新UI的时候发现关闭项目之后经常再次打开项目会出现一对的报错. 找了一圈,发现是ScrollBar控件出现了BUG.整个空间的滑块消失了. 在 ...
- Android——Activity初学
manifests里的AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> < ...
- pip和 easy_insall的区别
作为Python爱好者,如果不知道easy_install或者pip中的任何一个的话,那么...... easy_insall的作用和perl中的cpan,ruby中的gem类似,都提供了在线一键 ...