Javascript, solution-sharing


  • 1
    C
    [javascript]
    /**
     * step 1: find all matrix[i][j]===0, push i,j to the array
     * step 2: traversal array, change the value
     */
    var setZeroes = function(matrix) {
        var solution = [];
        for(var i=0; i<matrix.length; ++i){ // step 1
            for(var j=0; j<matrix[i].length; ++j){
                if(matrix[i][j]===0){
                    solution.push(i);
                    solution.push(j);
                }
            }
        }
        for(var k=0; k<solution.length; ++k){ // step 2
            for(j=0; j<matrix[solution[k]].length; ++j){
                matrix[solution[k]][j] = 0;
            }
            for(i=0; i<matrix.length; ++i){
                matrix[i][solution[k+1]] = 0;
            }
            ++k;
        }
    };

Log in to reply
 

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