Set Matrix Zeroes Problem Solution

Set Matrix Zeroes Problem: 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.

Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]

Set Matrix Zeroes Problem Solution

Problem Solution In Python

N=len(matrix)
        n=len(matrix[0])
        iarr=set()
        jarr=set()
 
        for i in range(N):
            for j in range(n):
                if matrix[i][j]==0:
                    if i not in iarr:
                        iarr.add(i)
                    if j not in jarr:
                        jarr.add(j)
                    
        for val in iarr:
            for j in range(n):
                matrix[val][j]=0
        for val in jarr:
            for i in range(N):
                matrix[i][val]=0

Problem Solution In Java

class Solution {
    public void setZeroes(int[][] matrix) {
        int fRow = 1, fCol = 1;
            
        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) {
                        fRow = 0;
                    }
                    if (j == 0) {
                        fCol = 0;
                    }
                    matrix[0][j] = 0;
                    matrix[i][0] = 0;
                }
            }
        }

        for(int i = 1; i < matrix.length; i++) {
            for(int j = 1; j < matrix[0].length; j++) {
                if(matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }
        if (fRow == 0) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[0][j] = 0;
            }
        }
        if (fCol == 0) {
            for (int i = 0; i < matrix.length; i++) {
                matrix[i][0] = 0;
            }
        }
    }
}

Problem Solution In C++

void setZeroes(vector<vector<int>>& matrix) {
        vector<pair<int,int>>a;
        int m = matrix.size();
        int n = matrix[0].size();
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(matrix[i][j]==0)
                    a.push_back(make_pair(i,j));
            }
        }
        
        for(auto e: a)
        {
            for(int j=0;j<n;j++)
            {
               if(matrix[e.first][j] != 0)
                   matrix[e.first][j]=0;
            }
            for(int i=0;i<m;i++)
            {
                if(matrix[i][e.second] != 0)
                    matrix[i][e.second]=0;
            }
        }
    }

Problem Solution In C

void setZeroes(int** matrix, int matrixSize, int* matrixColSize){

    int auxmat[matrixSize][(*matrixColSize)]; //auxmat is an auxillary matrix
    int i,j,k;
    for(i=0;i<matrixSize;i++)
    {
        for(j=0;j<*(matrixColSize);j++)
        {
            auxmat[i][j]=matrix[i][j];
        }
    }
    for(i=0;i<matrixSize;i++)
    {
        for(j=0;j<(*matrixColSize);j++)
        {
            if(matrix[i][j]==0)
            {
                for(k=0;k<matrixSize;k++)
                {
                    auxmat[k][j]=0;
                }
                
                for(k=0;k<(*matrixColSize);k++)
                {
                    auxmat[i][k]=0;
                }
            }
        }
    }
    for(i=0;i<matrixSize;i++)
    {
        for(j=0;j<(*matrixColSize);j++)
        {
            matrix[i][j]=auxmat[i][j];
        }
    }
}


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment