Set Matrix Zeroes

Problem: Set Matrix Zeroes

Since we cannot do in-place changing immediately since it will influence later operations, we can mark whether we need to set the rows or columns at start of rows and columns.

Code in Python:

class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        m, n, firstRowHasZero = len(matrix), len(matrix[0]), not all(matrix[0])
        for i in xrange(1, m):
            for j in xrange(n):
                if matrix[i][j] == 0:
                    matrix[0][j] = matrix[i][0] = 0
        for i in xrange(1, m):
            for j in xrange(n - 1, -1, -1):
                if matrix[i][0] == 0 or matrix[0][j] == 0:
                    matrix[i][j] = 0
        if firstRowHasZero:
            matrix[0] = [0] * n

results matching ""

    No results matching ""