Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.

Example 1:

Input:
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output:
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input:
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output:
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

Follow up:

  • 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?
思路

       这道题最简单的就是使用O(m*n)的空间来解决问题,但是空间浪费太高。我们可以先对矩阵进行遍历,将0出现的位置的行坐标和列坐标分别记录下来。然后当遍历完毕之后我们根据行坐标和列坐标来设置0.得到最后的结果。时间复杂度为O(m*n), 空间复杂度为O(m+n)。 另外在题中,提到使用O(1)的空间也可以解决问题,我没能想出来,看了参考答案之后明白了他在遇到元素0的时候,先将该元素所对应的行的第一个元素和对应列的第一个元素设置为0,然后遍历结束之后判断首行元素的的值状态将剩余的部分设置为0.最后得到结果。
 
解决代码

 class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
if not matrix:
return matrix
row, cloum = len(matrix), len(matrix[0])
tem_row, tem_cloum = set(), set()
for i in range(row): #遍历矩阵得到0的行坐标和列左边
for j in range(cloum):
if matrix[i][j] == 0:
tem_row.add(i)
tem_cloum.add(j)
self.set_zero(matrix, tem_row, tem_cloum) # 进行0设置 def set_zero(self, matrix, tem_row, tem_cloum):
for row in tem_row: # 设置列
for i in range(len(matrix[0])):
if matrix[row][i] != 0:
matrix[row][i] = 0
for cloum in tem_cloum: # 设置行
for j in range(len(matrix)):
if matrix[j][cloum] != 0:
matrix[j][cloum] = 0

常量空间的解法

  详细解释链接:https://leetcode.com/problems/set-matrix-zeroes/solution/

 class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
is_col = False
R = len(matrix)
C = len(matrix[0])
for i in range(R): # 遍历矩阵,得到元素为0的地址,并设置该列和该行起始位置为0,
if matrix[i][0] == 0:
is_col = True
for j in range(1, C):
if matrix[i][j] == 0:
matrix[0][j] = 0
matrix[i][0] = 0 for i in range(1, R): # 根据行的开头和列的开头来设置剩余部分的元素值
for j in range(1, C):
if not matrix[i][0] or not matrix[0][j]:
matrix[i][j] = 0 if matrix[0][0] == 0: # 判断起始位置是否为0
for j in range(C):
matrix[0][j] = 0 if is_col: # 第一列全为0
for i in range(R):
matrix[i][0] = 0

最新文章

  1. python 脚本中使用了第三方openpyxl 打包程序运行提示ImportError:cannot import name __version__
  2. Python爬虫抓取糗百的图片,并存储在本地文件夹
  3. flume与kafka整合
  4. js 如何在浏览器中获取当前位置的经纬度
  5. Facebook通过oAuth验证获取json数据
  6. SQLite常用网址
  7. IPy
  8. 部分实用的SQL语句
  9. C# 2 运算符 if
  10. mysql函数操作(6)
  11. 第1回-使用ThinkPHP的3.1.3版本轻松建网站
  12. Linux下LANMP集成环境中编译增加pdo_odbc模块
  13. opencv 图像矫正
  14. ]remove-duplicates-from-sorted-list-ii (删除)
  15. 【Codeforces Round 1132】Educational Round 61
  16. 把旧系统迁移到.Net Core 2.0 日记 (13) --图形验证码
  17. 教你用CMD命令查询域名的DNS解析记录:A,NS,MX,CNAME,TXT
  18. Python3.X如何下载安装urllib2包 ?
  19. C#阿里云 移动推送 接入
  20. Python基础学习----公共方法及运算符

热门文章

  1. 在python中使用print()时,raw write()返回无效的长度:OSError: raw write() returned invalid length 254 (should have been between 0 and 127)
  2. PHP中的面向对象思想
  3. eclipse查看一个方法被谁引用(调用)的快捷键四种方式
  4. C++中STL常用容器的优点和缺点
  5. python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为
  6. Linux远程登录ssh免密码配置方法(仅供参考)
  7. 第四篇flask中模板语言 jinja2
  8. 洛谷.5284.[十二省联考2019]字符串问题(后缀自动机 拓扑 DP)
  9. XVIII Open Cup named after E.V. Pankratiev. Ukrainian Grand Prix
  10. World Finals 2018 感想