# LeetCode-73 Set Matrix Zeroes

## Description

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

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

Constraints:
• m == matrix.length
• n == matrix[0].length
• 1 <= m, n <= 200
• -231 <= matrix[i][j] <= 231 - 1

## Example

Example 1:

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

Example 2:

Input: matrix = [[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]]

## Submissions

class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 0:
for k in range(len(matrix[0])):
matrix[i][k] = 0
for w in range(len(matrix)):
matrix[w][j]=0


class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m = len(matrix)
n = len(matrix[0])
row = [0 for _ in range(m)]
col = [0 for _ in range(n)]
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
row[i], col[j] = 1, 1
for i in range(m):
for j in range(n):
if row[i] or col[j]:
matrix[i][j] = 0

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

温暖℡

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-30 350

06-30 403
12-16 288
04-25 154
01-13 554
05-05 1269
03-25 405
04-13 424