题目: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?

这个问题使用额外的空间来完成比较直观,但是人要求用“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]的更多相关文章

  1. LeetCode 笔记系列 18 Maximal Rectangle [学以致用]

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  2. 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 ...

  3. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. 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. ...

  5. 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 ...

  6. 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 ...

  7. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

    题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...

  8. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  9. LeetCode 笔记系列五 Generate Parentheses

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

随机推荐

  1. xml去除指定节点,保留子节点操作

    现有xml文件: <?xml version="1.0" encoding="utf-8" ?> <ShiftConfig xmlns:xsi ...

  2. Oracle 10g如何对用户姓名,按首字母排序、查询

    首先介绍Oracle 9i新增加的一个系统自带的排序函数  1.按首字母排序  在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值      SCHINESE_RADICA ...

  3. C#如何判断操作系统位数是32位还是64位

    方法一: 对于C#来说,调用WMI是一种简单易行的方式.我们可以用Win32_Processor类里面的AddressWidth属性来表示系统的位宽.AddressWidth的值受CPU和操作系统的双 ...

  4. ie,cookie,域名与下划线

    前几天为了开发方便,和一个同事将XX.qq.com的测试站点拆成两个站点,我那个叫XX_hanks.qq.com,进行功能开发,在调试 cookie功能的时候,发现IE下cookie没有记住,用htt ...

  5. 微服务Spring Cloud与Kubernetes比较

    转 http://www.tuicool.com/articles/VnMf2y3 Spring Cloud或Kubernetes都宣称它们是开发运行微服务的最好环境,哪个更好?答案是两个都是,但他们 ...

  6. 【Android】16.0 第16章 自定义服务和系统服务—本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-03-01 一.简介 本章主要演示Started Service.带Intent过滤器的Started Service.IntentSe ...

  7. Spring3的表达式语言

    Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言, 能在运行时构建复杂表达式.存取对象图属性. ...

  8. Makefile学习之路6——让编译环境更加有序

    在大多项目中都会合理设计目录结构来提高维护性,在编译一个项目时会产生大量中间文件,如果中间文件直接和源文件放在一起,就显得杂乱而不利于维护.在为现在这个complicated项目编写makefile之 ...

  9. 5分钟用Spring4 搭建一个REST WebService(转)

    章节目录 前置技能 新建项目,配置依赖文件 编写Model和Controller 启动服务&访问 但是 其他 前置技能 ① 使用maven来管理java项目 这个技能必须点一级,以便快速配置项 ...

  10. fastjson常用操作

    一. fastjson生成json字符串(JavaBean,List<JavaBean>,List<String>,List<Map<String,Object&g ...