Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
开辟两个数组,做标记,出现0,就记录,二次遍历时,就全行,全列置0
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(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
m = len(matrix)
n = len(matrix[0])
row = [False for i in range(m)]
colum = [False for j in range(n)]
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
row[i] = True
colum[j] = True
for i in range(m):
for j in range(n):
if row[i] or colum[j]:
matrix[i][j] = 0