easy to read python solution O(m) space where m is number of columns to set to 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.
            cols = set()
            set_row = False
            for r in range(len(matrix)):
                row = matrix[r]
                for i in range(len(row)):
                    if row[i] == 0:
                        set_row = True
                if set_row:
                    matrix[r] = map(lambda x: 0, row)
                    set_row = False
            for row in matrix:
                for i in cols:
                    row[i] = 0

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.