Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

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?

SOLUTION 1:

题目要求O(1)的空间消耗。

1. 我们可以使用第一行,第一列来作为Flag,记录某一行,某一列是否应该被设置为0.

2. 因为第一行,第一列共用左上角的flag,所以我们需要另外找2个flag来定义第一行,第一列本身是否应该设置为0.

row1Zero, col1Zero

3. 先扫描首行,首列把首行首列的flag算出。

4. 扫描其他的矩阵,将第一行每一列的flag算出。

5. 设置矩阵中除了首行首列的cells.

6. 设置首行,设置首列。

 public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length ==
|| matrix[].length == ) {
return;
} boolean row1Zero = false;
boolean col1Zero = false; int rows = matrix.length;
int cols = matrix[].length; // Determine if the first column should be Zero.
for (int i = ; i < rows; i++) {
if (matrix[i][] == ) {
col1Zero = true;
break;
}
} // Determine if the first row should be Zero.
for (int i = ; i < cols; i++) {
if (matrix[][i] == ) {
row1Zero = true;
break;
}
} // we use the first row and the first col as the flag to record the
// cells whether or not set to 0.
for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
// 注意了,这个矩阵是0和非0,并不是0和1.
if (matrix[i][j] == ) {
// set the flag in the first line and the first column
matrix[i][] = ;
matrix[][j] = ;
}
}
} // set the inner cells.
// Be careful: i, j start from 1.
for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
if (matrix[i][] ==
|| matrix[][j] == ) {
matrix[i][j] = ;
}
}
} // set the first row.
if (row1Zero) {
for (int i = ; i < cols; i++) {
matrix[][i] = ;
}
} // set the first col.
if (col1Zero) {
for (int i = ; i < rows; i++) {
matrix[i][] = ;
}
} return;
}
}

2014.1230 redo:

把前面三个循环合并,会简单一点儿。

 public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
} boolean row1 = false;
boolean col1 = false; int rows = matrix.length;
int cols = matrix[0].length; // set flags.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
continue;
} // set the flag of a column and a row.
matrix[0][j] = 0;
matrix[i][0] = 0; // get flag of first row.
if (i == 0) {
row1 = true;
} // get flag of first column.
if (j == 0) {
col1 = true;
}
}
} // set the matrix.
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
} // set first column
if (col1) {
for (int i = 0; i < rows; i++) {
// bug 1: can't use matrix[i][j]
matrix[i][0] = 0;
}
} // set first row.
if (row1) {
for (int i = 0; i < cols; i++) {
matrix[0][i] = 0;
}
}
}
}

GitHub Code:

SetZeroes.java

LeetCode: Set Matrix Zeroes 解题报告的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

  3. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  4. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  5. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. 〖Linux〗VIM youcompleteme 自动补全 #include 文件名称

    1. 拷贝配置文件 cp ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py ~/.vim/.ycm_extra_conf.py 2. 修改配 ...

  2. python之模块csv之CSV文件一次写入多行

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #CSV文件一次写入多行 import csv #csv文件,是一种常用的文本格式,用以存储表格数据,很多程序 ...

  3. 2、classpath、path、JAVA_HOME的作用

    windows xp下配置JDK环境变量: 1.安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为D:\java\jdk1.5.0_08: 2.安装完成后,右击“我的电脑”,点击“ ...

  4. linux显示桌面快捷键设置

    2013-01-06 10:31:52    Ubuntu显示桌面Indicator  IN: LINUX :-) HOT: 1,246 ℃  18十2011   www.2cto.com   大家一 ...

  5. 谷歌地图地理解析和反解析geocode.geocoder详解(转)

    谷歌地图地理解析和反解析geocode.geocoder详解 谷歌Geocoder服务 实例代码 地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. ...

  6. Android学习系列(12)--App列表之拖拽GridView

    根据前面文章中ListView拖拽的实现原理,我们也是很容易实现推拽GridView的,下面我就以相同步骤实现基本的GridView拖拽效果.     因为GridView不用做分组处理,代码处理起来 ...

  7. 关于ftpshell脚本中mget中去除多余交互式提示的方法

    默认情况下ftp里面的交互式提示是开启的,平常如果下载多个文件时,这种提示很让人烦,如果是在shell脚本里面要从ftp服务器上一次mget多个文件,写个交互式很麻烦,最好是把这个交互式提示关掉. 进 ...

  8. Linux内核同步 - Per-CPU变量

    一.源由:为何引入Per-CPU变量? 1.lock bus带来的性能问题 在ARM平台上,ARMv6之前,SWP和SWPB指令被用来支持对shared memory的访问: SWP <Rt&g ...

  9. tp查询范围

    一.查询范围 //说明username和password是对应模型下的  scopeUsername($query) 和scopePassword($query)方法 //关键字scope 在控制器如 ...

  10. PHP通过__call实现简单的AOP(主事务后的其他操作)比如前置通知,后置通知

    /** * person class */ class Person { /** * person class -> function say */ public static function ...