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 ...
随机推荐
- PHP SOCKET编程(未完)
转 http://blog.csdn.net/hguisu/article/details/7448528
- swift的UITableView的使用
UITableView是app开发中经常使用到的控件,功能非常强大,多用于数据的显示. 以下以一个简单的实例来介绍其基本使用方法. 先建一个project 代码例如以下: import UIKit c ...
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- mysql 索引 大于等于 走不走索引 最左前缀
你可以认为联合索引是闯关游戏的设计 例如你这个联合索引是state/city/zipCode 那么state就是第一关 city是第二关, zipCode就是第三关 你必须匹配了第一关,才能匹配第二关 ...
- ewebeditor编辑器配合IIS6.0解析漏洞拿shell
很明显这是一个ewebeditor编辑器,这个编辑器存在可遍历目录可创建文件夹等一系列漏洞.直接在url处加../即可.若要创建文件夹直接在url后面写文件夹名称即可. 上传一张shell图片,抓包改 ...
- [misc]如何在嵌入式平台使用printf功能
转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html 摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上, ...
- 如何对MySQL中的大表进行数据归档
使用MySQL的过程,经常会遇到一个问题,比如说某张”log”表,用于保存某种记录,随着时间的不断的累积数据,但是只有最新的一段时间的数据是有用的:这个时候会遇到性能和容量的瓶颈,需要将表中的历史数据 ...
- mq和redis安装
[root@129-2-10-8 src]# cat b.sh #!/bin/bash ####install redis software #####echo "############# ...
- 如何让jquery-easyui的combobox像select那样不可编辑
http://zhidao.baidu.com/link?url=td61iIn_MBCs1FvT7b-B9Lp9VzlyrcnGmSbkCy1EsSzuod5o47zTmJFRQ-xizxdqv1E ...
- Toad 所有 菜单说明(太多)
菜单说明 新版本 toad 软件中, 比较有用的菜单 session 菜单 Session Information: 显示当前session的用户的情况, 比如权限, 授权等 Database ...