题目

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?

 
代码:oj测试通过 Runtime: 220 ms
 class Solution:
# @param matrix, a list of lists of integers
# RETURN NOTHING, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
# none case
if matrix is None:
return None
# dimension of the matrix
ROW = len(matrix)
COL = len(matrix[0])
# record the status of first row and first column
first_row_contains_zero = False
for i in range(COL):
if matrix[0][i] == 0:
first_row_contains_zero = True
first_col_contains_zero = False
for i in range(ROW):
if matrix[i][0] == 0:
first_col_contains_zero = True
# visit the ROW-1 × COL-1 matrix and check zeros
for row in range(1,ROW):
for col in range(1,COL):
if matrix[row][col] == 0:
matrix[row][0] = 0
matrix[0][col] = 0
# set zero rows and zero columns
for row in range(1,ROW):
for col in range(1,COL):
if matrix[row][0]==0 or matrix[0][col]==0:
matrix[row][col] = 0
if first_row_contains_zero:
for i in range(COL):
matrix[0][i] = 0
if first_col_contains_zero:
for i in range(ROW):
matrix[i][0] = 0

思路

题目难点在于要求使用常数空间。

方法是先记录矩阵第一列和第一行是否含有0元素;然后利用第一行和第一列来作为其余列和行是否含有0的记录位置,这样就省下了O(m+n)的空间。

这里有一个梗需要说明:

比如matrix[0][j]作为矩阵第j列是否含有0的标志位,这里分两种情况:

1. matrix[0][j]本身为0,则整个j列的元素(包括matrix[0][j])最后都为0

2. matrix[0][j]不为零,如果j列其余元素有0,则matrix[0][j]为0;如果j列其余元素不含有0,则matrix[0][j]保持原来的值也不为零

相同了上面的trick之后就可以把代码写出来AC了。

主要学习如下的日志:

http://blog.sina.com.cn/s/blog_45b8132001018xie.html

另外,再狗尾续貂,把自己写的第一版代码也附上。

oj测试通过 Runtime: 213 ms

class Solution:
# @param matrix, a list of lists of integers
# RETURN NOTHING, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
# none case
if matrix is None:
return None
# dimension of the matrix
ROW = len(matrix)
COL = len(matrix[0])
# record zero rows and zero columns
zero_row = [False for i in range(ROW)]
zero_col = [False for i in range(COL)]
for row in range(ROW):
for col in range(COL):
if matrix[row][col] == 0:
zero_row[row] = True
zero_col[col] = True
# set zero rows and zero columns
for row in range(ROW):
for col in range(COL):
if zero_row[row] or zero_col[col]:
matrix[row][col] = 0

感觉这两种代码在效率上差不多。无非就是时间空间互换的问题。

不过既然只牺牲很少的时间,就换来了一些空间,也是不错的,以后可以在工作中记住这个trick。

leetcode 【 Set Matrix Zeroes 】python 实现的更多相关文章

  1. [leetcode]Set Matrix Zeroes @ Python

    原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...

  2. LeetCode: Set Matrix Zeroes 解题报告

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

  3. [LeetCode] 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 ...

  4. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  5. LeetCode OJ--Set Matrix Zeroes **

    http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...

  6. LeetCode——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. 原题链接:h ...

  7. LeetCode Set Matrix Zeroes(技巧+逻辑)

    题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...

  8. Leetcode 283 Move Zeroes python

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

  9. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  10. [Leetcode] 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 ...

随机推荐

  1. Spring MVC的测试

    测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...

  2. 如何使用VS将项目生成一个安装包?

    VS2010项目的部署与安装winform程序,我想进行安装.1.在解决方案中 ——点击右键——添加 2.然后选择 安装和部署 ——安装向导 可以更改名称 3.点击 下一步 4.然后选择上那3个 5. ...

  3. linux日常1-踢出用户

    踢掉自己不用的终端 1.查看系统在线用户 w 2.查看哪个属于此时自己的终端(我开了两个连接) who am i 3.pkill掉自己不适用的终端 pkill -kill -t pts/1 注意: 如 ...

  4. 测试MS题

    购物车测试点:  1.界面测试        界面布局.排版是否合理:文字是否显示清晰:不同卖家的商品是否区分明显. 2.功能测试 未登录时: 将商品加入购物车,页面跳转到登录页面,登录成功后购物车数 ...

  5. Visual Studio 2015 Preview 使用中问题一枚

    只要碰到IO读写,文件不存在之类的系统异常,就会崩溃一下给你看看.直接重新VS. 不该有的问题确实存在着???? 正常情况是这样的 直接崩溃时万万不行的!!!!

  6. linux 命令——16 which(转)

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.         ...

  7. Drupal7强制把主题恢复到默认主题

    今天在写Theme,退出登陆的时候无法进入管理后台. 万不得已之下只有使用数据库进行恢复. Rest Drupal Theme to Bartik SQL语句如下: WHERE type = 'the ...

  8. React Native 初探

    推荐文章 React Native 简介:用 JavaScript 搭建 iOS 应用 (1) React Native 简介:用 JavaScript 搭建 iOS 应用 (2) React Nat ...

  9. es6中的类及es5类的实现

    目录 类的特点 类的特点 1.类只能通过new得到 在es6中类的使用只能是通过new,如果你将它作为一个函数执行,将会报错. //es6的写法 class Child { constructor() ...

  10. C++大数问题

    1.大数的加法 语法:add(char a[],char b[],char s[]); 参数: a[]:被加数,用字符串表示,位数不限 b[]:加数,用字符串表示,位数不限 s[]:结果,用字符串表示 ...