Leetcode Set Matrix Zeroes Problem Solution

In this Leetcode Set Matrix Zeroes Problem Solution Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

You must do it in place.

Leetcode Set Matrix Zeroes Problem Solution


Problem solution in Python.

class Solution:
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        if matrix is None or len(matrix) == 0:
            return
        
        rowOneZero = False 
        colOneZero = False
        
        for i in range(0,len(matrix)):
            for j in range(0,len(matrix[0])):
                if matrix[i][j] == 0:
                    if i == 0:
                        rowOneZero = True
                    if j == 0:
                        colOneZero = True
                    matrix[0][j] = 0
                    matrix[i][0] = 0
        
        #process the first col.
        for i in range(1,len(matrix)):
            if matrix[i][0] == 0:
                self.setRowZeros(matrix,i)
        #process the first row
        for j in range(1,len(matrix[0])):
            if matrix[0][j] == 0:
                self.setColZeros(matrix,j)
        #process the first row.
        if rowOneZero:
            self.setRowZeros(matrix,0)
        #process the first col
        if colOneZero:
            self.setColZeros(matrix,0)
        
    
    def setRowZeros(self,matrix,row):
        for j in range(0,len(matrix[0])):
            matrix[row][j] = 0
    
    def setColZeros(self,matrix,col):
        for i in range(0,len(matrix)):
            matrix[i][col] = 0

Problem solution in Java.

class Solution {
    public void setZeroes(int[][] matrix) {
        //check for edge cases
        if(matrix == null || matrix.length == 0){
            return;
        }
        
        boolean rowOneZero = false;
        boolean colOneZero = false;
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[i][j] == 0){
                    if(i==0){
                        rowOneZero = true;
                    }
                    if(j==0){
                        colOneZero = true;
                    }
                    //mark the first row
                    matrix[0][j] = 0;
                    //mark the first col
                    matrix[i][0] = 0;
                }
            }
        }
        //process the first row, zero the appropriate col's
        //exclude processing the first col
        for(int j=1;j<matrix[0].length;j++){
            if(matrix[0][j] == 0){
                setColZeros(matrix,j);
            }
        }
        //process the first col, zero the appropriate row's
        //exclude processing the first row
        for(int i=1;i<matrix.length;i++){
            if(matrix[i][0] == 0){
                setRowZeros(matrix,i);
            }
        }
        //process the first row, if it had any zeros
        if(rowOneZero){
            setRowZeros(matrix,0);
        }
        //process the first col, if it had any zeros
        if(colOneZero){
            setColZeros(matrix,0);
        }
    }
    
    public void setColZeros(int[][] matrix,int col){
        for(int i=0;i<matrix.length;i++){
            matrix[i][col] = 0;
        }
    }
    
    public void setRowZeros(int[][] matrix, int row){
        for(int j=0;j<matrix[0].length;j++){
            matrix[row][j] = 0;
        }
    }
    
}


Problem solution in C++.

class Solution {
public:
    void setZeroes(vector<vector<int>>& mat) {
        int row=mat.size(),col=mat[0].size();
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(mat[i][j]==0){
                    int ind=i-1;
                    while(ind>=0){
                        if(mat[ind][j]!=0){
                            mat[ind][j]=INT_MIN/10;
                        }
                        ind--;
                    }
                    ind=i+1;
                    while(row>ind){
                        if(mat[ind][j]!=0){
                            mat[ind][j]=INT_MIN/10;
                        }
                        ind++;
                    }
                    ind = j-1;
                    while(ind>=0){
                        if(mat[i][ind]!=0){
                            mat[i][ind]=INT_MIN/10;
                        }
                        ind--;
                    }
                    ind=j+1;
                    while(ind<col){
                        if(mat[i][ind]!=0){
                            mat[i][ind]=INT_MIN/10;
                        }
                        ind++;
                    }
                }
            }
        }
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(mat[i][j]==INT_MIN/10){
                    mat[i][j]=0;
                }
            }
        }
    }
};


Problem solution in C.

void setZeroes(int** matrix, int matrixSize, int* matrixColSize){
    int i = 0, j=0, k=0;
    int col = *matrixColSize;
    int temp_array[matrixSize][col];
    memset(temp_array, 0, sizeof(temp_array));
    
    // save the locations of zeros in the temp_array
    for (i=0;i<matrixSize;i++) {
        for (j=0;j<col;j++) {
            if (matrix[i][j] == 0)
                temp_array[i][j] = 1;
        }
    }
    
    for (i=0; i<matrixSize; i++) {
        for (j=0;j<col;j++) {
            if (temp_array[i][j] == 1) {
                // set the row i to 0
                for(k=0; k<col;k++) {
                    matrix[i][k] = 0;
                }
                // set the col j = 0
                for (k=0; k<matrixSize; k++) {
                    matrix[k][j] = 0;
                }
            }
        }
    }
 
}


Post a Comment

0 Comments