Python solution, O(mn) time, O(s) space, where s is number of zeroes


  • -1
    K
    class Solution:
    # @param matrix, a list of lists of integers
    # @return nothing (void), do not return anything, MODIFY matrix IN PLACE.
    def setZeroes(self, matrix):
        ixs = {
                'rows': [],
                'cols': []
              }
        
        for rix,row in enumerate( matrix ):
            for cix,val in enumerate( row ):
                if val == 0:
                    ixs['rows'].append( rix )
                    ixs['cols'].append( cix )
                
        for rix in ixs['rows']:
            for cix,val in enumerate( matrix[rix] ):
                matrix[rix][cix] = 0
                
        for cix in ixs['cols']:
            for rix,row in enumerate( matrix ):
                matrix[rix][cix] = 0

Log in to reply
 

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