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 ...
随机推荐
- Django的ORM中如何判断查询结果是否为空,判断django中的orm为空
result= Booking.objects.filter() #方法一 .exists() if result.exists(): print "QuerySet has Data&qu ...
- mysql的join操作
一.Join语法概述 join 用于多表中字段之间的联系,语法如下: ... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona table1 ...
- JStorm环境搭建
开始JStorm学习之前需要搭建集群环境,这里演示搭建单机JStorm环境,仅供学习使用,生产环境部署大同小异,但建议参考JStorm社区及相关说明文档. 一.前提 JStorm核心代码均用Java实 ...
- CentOS 删除桌面环境
帮客户买了一个vps, 结果里面装了一堆没用的软件,所以全部删掉 CentOS 桌面安装大多都是 以软件包的 形式安装 所以 最好是设置好 国内的yum 源, 然后执行: >yum groupl ...
- IIS6 伪静态 IIS文件类型映射配置方法 【图解】
1.右键点击 要设置网站的网站 2.属性 -->主目录 -->配置--> 3.如右侧窗口,找到 .aspx 扩展名-->编辑-->复制 可执行文件的路径-->关闭 ...
- C语言复杂声明解读简明方法
//char (*(*x[3])())[5];//x是什么类型的变量? // //分析C语言声明,关键是搞清楚这个变量是个什么东西(函数.指针.数组), //是函数那么剩下的就是他的参数和返回值, / ...
- vim在vps内的终端内支持molokai
vps的终端内默认的颜色数好像很低.对molokai的支持一直不好. 后查找后得知:vim终端方式默认为16色,而molokai为256配色方案 我以为这是硬件问题,没有解决办法,一直到有一天,我在配 ...
- Atitit.c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结
Atitit.c# .net 3.5 4.0 各个版本新特性战略规划总结 1. --------------.Net Framework版本同CLR版本的关系1 2. paip.----------- ...
- SMBUS的介绍与访问
博文是为了总结自己在bios学习上面的点点滴滴,并且加深印象,由于本人水平有限,难免存在不足之处,望指正,同时感谢CSDN提供的平台.本文主要介绍的是SMBUS. 1 SMBUS的简介 特点: SM ...
- 【Android】13.3 使用SQLite.NET-PCL访问SQLite数据库
分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 本章开头已经说过了,SQLite.NET-PCL用起来很爽,这一节咱们看看怎样使用吧. 二.示例3运行截图 下面左 ...