题目

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. Garmin APP开发之入门

    Garmin开发-入门 先附上几个已经开发完成的app日历 up down 翻月 start 回到当前月(就差农历了) 秒表和定时器一体app界面比较简单,但是实用,长按菜单键可以切换秒表和定时器,有 ...

  2. MVC+Nhibernate+jquery+easyui递归实现多级菜单

    1.新建访问的控制器动作返回视图,在视图中使用easyui的treegrid插件来得到后台得到的json数据显示多级菜单 public ActionResult Menu() { return Vie ...

  3. 景安快云VPS挂载数据盘至指定目录 使得系统与数据分离

    如果我们细心的用户会发现购买景安快云VPS主机后,通过df检测看到系统盘大小与我们购买时候给的不一样,这个是很正常的事情.一般VPS主机商会通过给予系统盘和数据盘一并的数据磁盘给我们,但是默认我们看到 ...

  4. C++,C++编程,Windows编程,MFC

    编程 我们日常生活中接触到的电子类产品中的应用都是由编程而来 为什么编程,偷懒 我们通过编程驱使(指挥,命令)的是电信号 为什么上面说编程是偷懒,电的发现,给人们带来了便利,人们在各个方面驱使(换成“ ...

  5. 弹出页面第一次加载可以生成table和方法的绑定,第二次点击进来不能生成table和方法的帮定

    问题原因: 弹出页面的写法是每次点击都会在原有页面基础之上新添加一个将其覆盖,原有页面不关闭.我用的生成table和点击事件的绑定是id选择器.页面中只绑定第一次的页面,第二次的页面作用不上. 解决: ...

  6. Android多媒体框架总结(1) - 利用MediaMuxer合成音视频数据流程分析

    场景介绍: 设备端通过服务器传向客户端(Android手机)实时发送视频数据(H.264)和音频数据(g711a或g711u), 需要在客户端将音视频数据保存为MP4文件存放在本地,用户可以通过APP ...

  7. yield 生成器的运行机制

    yield 生成器的运行机制 当你问生成器要一个数时,生成器会执行,直至出现 yield 语句,生成器把 yield 的参数给你,之后生成器就不会往下继续运行. 当你问他要下一个数时,他会从上次的状态 ...

  8. C#自减运算符

    一.C#自减运算符(--) 自减运算符(--)是将操作数减1. 1. 前缀自减运算符 前缀自减运算符是“先减1,后使用”.它的运算结果是操作数减1之后的值. 例如: --x;  // 前缀自减运算符 ...

  9. iOS 闭包传值 和 代理传值

    let vc = ViewController() let navc = UINavigationController(rootViewController: vc) window = UIWindo ...

  10. C++大数问题

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