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

【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)的更多相关文章

  1. LeetCode(73)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. cli ...

  2. LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]

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

  3. LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

    题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...

  4. leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)

    题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 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. 题解:因为题 ...

  7. [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 ...

  8. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  9. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

随机推荐

  1. Windows Internals 笔记——字符和字符串处理

    1.自Windows NT起,Windows的所有版本都完全用Unicode来构建,调用Windows函数时,如果向它传入一个ANSI字符串,那么函数首先会把字符串转换为Unicode,再把结果传给操 ...

  2. 金蝶K/3 同步用核算项目配置

  3. mysql字段有中英文,数字按照升序/降序 排序

    ORDER BY    CONVERT(name,SIGNED) ASC,    CONVERT(name USING gbk) DESC

  4. 洛古P1036 选数 题解

    [我是传送门] 这是一道很经典的深搜与回溯(难度一般) 可是就这个"普及-" 让本蒟蒻做了一晚上+半个上午(实际我不会深搜回溯,全靠框架+去重); 下面让我分享下本蒟蒻的(全排列+ ...

  5. Anaconda3 tensorflow安装 及ModuleNotFoundError: No module named 'tensorflow' 解答

    Anaconda3 的安装,参考:手把手教你如何安装Tensorflow(Windows和Linux两种版本) tensorflow的安装,参考:深度学习(TensorFlow)环境搭建:(三)Ubu ...

  6. 网络流之最大流Dinic算法模版

    /* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...

  7. 项目导入之后报错:The import javax.servlet cannot be resolved

    项目导入之后报错:The import javax.servlet cannot be resolved 解决方法:在Eclipse中,右击项目,选择Build Path->configure ...

  8. angular.isNumber()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. flask之wtforms

    本篇导航: wtforms组件的使用 自定义From组件 一.wtforms组件的使用 1.flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进 ...

  10. error MSB8020 问题解决

    产生原因: 1.vs 版本过低 2.项目平台工具选择不正确 解决方案: 1.安装VS2015以上的版本 2.选择项目属性,修改平台工具,选择当前版本可用的工具. 具体步骤:右键点击你的项目,选择 Pr ...