A Python O(m+n) solution

  • 1
    class Solution:
        # @param matrix, a list of lists of integers
        # @return nothing (void), do not return anything, MODIFY matrix IN PLACE.
        # 12:35
        def setZeroes(self, matrix):
            zeroRows = set()
            zeroCols = set()
            for i in range(len(matrix)):
                for j in range(len(matrix[0])):
                    if matrix[i][j] == 0:
            for row in zeroRows:
                matrix[row][:] = map(lambda x:0, matrix[row])
            for i in range(len(matrix)):
                for col in zeroCols:
                    matrix[i][col] = 0

Log in to reply

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