题目

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. Java字节流与字符流的区别详解

    字节流与字符流 先来看一下流的概念: 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入 ...

  2. MobaXterm连接远程Linux服务器

    MobaXterm是一个X服务器和一组的Unix命令(GNU/ Cygwin的)封装在一个单一的便携式exe文件的增强终端. MobaXterm包括一个巨大的multitab原生的Windows终端. ...

  3. JS案例练习-手机微信聊天对话框

    先附图 CSS部分: <style> body{} *{;} li{list-style: none;} .container{ width:310px; height:600px; ma ...

  4. MyBatis源码探索

    每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的.SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获 ...

  5. linux 命令——58 ss(转)

    telnet 命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是 Internet远程登陆服务的标准协议和主要方式.它为 ...

  6. ABAP Netweaver和git的快捷方式

    Netweaver Jerry的SAPGUI收藏夹管理工具:链接 git 我笔记本上有很多github仓库,每次切换仓库,我不想敲很长的cd命令.比如现在我需要手敲下面的命令进入一个Java仓库: c ...

  7. 关于添加IBDesignable实时building很卡问题的解决

    公共库加了@IBDesignable,可以实时预览图标 但是有个问题,每次打开XIB的时候都会Building,会造成电脑会很卡 有个解决方法,在xcode的Editor下拉框取消auto refre ...

  8. spring-autowire机制

    在xml配置文件中,autowire有5种类型,可以在<bean/>元素中使用autowire属性指定 模式                        说明 no            ...

  9. Python求包含数字或字母最长的字符串及长度

    一.求包含数字或字母最长的字符串及长度 org = 'ss121*2222&sdfs2!aaabb' result = [] #保存最终要输出的字符串 result_temp = [] #保存 ...

  10. java打包打包

    http://blog.sina.com.cn/s/blog_6b9dcc870101k8xq.html 上面说的最后一种方法,不太对. 下面这个可以 Try the fat-jar extensio ...