leetcode 【 Set Matrix Zeroes 】python 实现
题目:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
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?
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 实现的更多相关文章
- [leetcode]Set Matrix Zeroes @ Python
原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...
- 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 ...
- [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 ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- LeetCode OJ--Set Matrix Zeroes **
http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...
- 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 ...
- LeetCode Set Matrix Zeroes(技巧+逻辑)
题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...
- 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 ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- [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 ...
随机推荐
- ArcGIS for Server新建站点异常,Failed to create the site.Failed to configure the server machine'XXXX',Server machine'XXXX' is not a local server machine.
系统环境:操作系统Win7 64位,装在虚拟机VM中,ArcGIS for Server 10.2.1 问题描述:ArcGIS for Server 10.2.1安装并授权完成后,站点初始化时显示 ...
- 中标麒麟高级服务器操作系统V6
平台: linux 类型: 虚拟机镜像 软件包: java-1.6.0 mysql-5.1.5 python-2.6 qt3-3.3.8b basic software linux neokylin ...
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- ubuntu 自动启动程序
首先打开终端ctrl + alt + t sudo -i 输入密码:ubuntu chmod 777 /etc/rc.local 打开 vi /etc/rc.local 按 i 键进入输入 ...
- 关于android界面菜单,project显示问题
刚用android studio不久,遇到了一个问题:界面菜单project不小心被关掉了,百度各种搜索无结果.我想大多数人都遇到过类似问题,此类问题其实很简单但是对于初学者来说就有点苦恼了.所以在此 ...
- python内置函数map/reduce/filter
python有几个内置的函数很有意 思:map/filter/reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是python列表方法的三架 ...
- C++手写快读详解(快速读入数字)
众所周知,C++里是自带读入的(这不废话吗) 例如: int a; cin>>a; 这样的读入理解简单,适合初学者,但是非常慢. 再例如: int a; scanf("%d&qu ...
- Map和Set -----JavaScript
本文摘要:http://www.liaoxuefeng.com/ JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaSc ...
- AngularJS 应用
AngularJS模块(Module)定义了AngularJS的应用. AngularJS控制器(Controller)用于控制AngularJS应用. ng-app指令定义了应用,ng-contro ...
- 黑马基础阶段测试题:通过字符输入流读取info.txt中的所有内容,每次读取一行,将每一行的第一个文字截取出来并打印在控制台上。
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...