Real O(1) Python code. No type change. Using first 0 occurrence as the placeholder


  • 1
    X
    class Solution:
    # @param matrix, a list of lists of integers
    # RETURN NOTHING, MODIFY matrix IN PLACE.
    def setZeroes(self, matrix):
        pilot = None
        m, n = len(matrix), len(matrix[0])
        for i in xrange(m):
            for j in xrange(n):
                if (matrix[i][j] == 0) and (pilot is None):
                    pilot = (i, j)
                elif matrix[i][j] == 0:
                    matrix[pilot[0]][j] = 0
                    matrix[i][pilot[1]] = 0
        if pilot is None : return
        
        for i in xrange(m):
            if i == pilot[0] : continue
            for j in xrange(n):
                if j == pilot[1]: continue
                if matrix[i][pilot[1]] == 0 or matrix[pilot[0]][j] == 0:
                    matrix[i][j] = 0
        
        for i in xrange(m):
            matrix[i][pilot[1]] = 0
        for j in xrange(n):
            matrix[pilot[0]][j] = 0

  • 0
    S

    awesome codes


Log in to reply
 

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