题目

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. 华为服务器操作系统EulerOS V2.0

    平台: linux 类型: 虚拟机镜像 软件包: java-1.8.0 php-5.4.16 python-2.7.5 qt-4.8.5 tomcat-7.0.69 basic software eu ...

  3. Navicat for Oracle设置唯一性和递增序列

    [数据库] Navicat for Oracle基本用法图文介绍 一. 设置唯一性 参考文章:Oracle之唯一性约束(UNIQUE Constraint)用法详解唯一性约束英文是Unique Con ...

  4. Oracle数据库基础--建表语法+操作

    语法 1.建表 create table 表名( 列名 数据类型, …… ); 2.删除表:drop table 表名; 3.添加列:alter table 表名 add(列名 数据类型); 4.修改 ...

  5. AD7190的小总结

    1.单次转换模式 通过配置“模式寄存器的MD2.MD1.MD0为001”,便可启动单次转换. 流程“上电 -> 单次转换 -> 省电模式 ” , 片内振荡上电需要大约1ms.   单次转换 ...

  6. UVA Stacks of Flapjacks 栈排序

    题意:给一个整数序列,输出每次反转的位置,输出0代表排序完成.给一个序列1 2 3 4 5,这5就是栈底,1是顶,底到顶的位置是从1~5,每次反转是指从左数第i个位置,将其及其左边所有的数字都反转,假 ...

  7. c++ STL list容器成员函数

    list是一个双链表. 函数 描述 void l.assign (int n, const val) void l.assign (it first, it last) 将链表l初始化为n个相同的va ...

  8. windows添加快速启动栏

    步骤: 右击任务栏——选择“新建工具栏” 在“文件夹”路径中填入%appdata%\Microsoft\Internet Explorer\Quick Launch并单点“选择文件夹” 右键单击任务栏 ...

  9. coursera 算法二 week 1 wordnet

    这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...

  10. java Vamei快速教程13 String类

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前的Java基础系列中讨论了Java最核心的概念,特别是面向对象的基础.在Jav ...