LeetCode||73. Set Matrix Zeroes

来源:互联网 发布:苹果mac怎么下载软件 编辑:程序博客网 时间:2024/06/15 19:51

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:

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?

开辟两个数组,做标记,出现0,就记录,二次遍历时,就全行,全列置0

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