Leetcode Set Matrix Zeroes problem solution

In the 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.

Example 1:

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

Example 2:

Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

Constraints:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -231 <= matrix[i][j] <= 231 – 1

Solution in C Programming

void setZeroes(int** matrix, int matrixSize, int* matrixColSize){
  bool row=false;
  bool column=false;
  for(int i=0;i<matrixSize;i++){
      if(matrix[i][0]==0){
          column=true;
          break;
      }
  }
  for(int j=0;j<*matrixColSize;j++){
      if(matrix[0][j]==0){
          row=true;
          break;
      }
  }
  for(int i=1;i<matrixSize;i++){
     for(int j=1;j<*matrixColSize;j++){
         if(matrix[i][j]==0){
             matrix[0][j]=0;
             matrix[i][0]=0;
         }
     }

  }
  for(int i=1; i<matrixSize; i++)
        for(int j=1;j<*matrixColSize; j++)
        {
            if(matrix[0][j] == 0 || matrix[i][0] == 0)
            {
                matrix[i][j] = 0;
            }
        }
        if(row){
            for(int i=0;i<*matrixColSize;i++)
            {
                matrix[0][i]=0;
            }
        }
        if(column){
            for(int i=0;i<matrixSize;i++)
            {
                matrix[i][0]=0;
            }
        }

}

Solution in C++ Programming

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        set<pair<int, int>> zeroCoords;
        for(int i = 0; i < matrix.size(); i++){
            for(int j = 0; j < matrix.at(i).size(); j++){
                if(matrix.at(i).at(j) == 0) zeroCoords.insert({i, j});
            }
        }
        
        for(int i = 0; i < matrix.size(); i++){
            for(int j = 0; j < matrix.at(i).size(); j++){
                if(matrix.at(i).at(j) == 0 && zeroCoords.find({i, j}) != zeroCoords.end()){
                    fillRows(matrix, i);
                    fillColumns(matrix, j);
                    zeroCoords.erase({i, j});
                }
            }
        }
    }
    
    void fillRows(vector<vector<int>> &matrix, int row){
        for(int j = 0; j < matrix.at(0).size(); j++)
            matrix.at(row).at(j) = 0;
    }
    
    void fillColumns(vector<vector<int>> &matrix, int col){
        for(int i = 0; i < matrix.size(); i++)
            matrix.at(i).at(col) = 0;
    }
};

Solution in Java Programming

class Solution {
    public void setZeroes(int[][] matrix) {
        int r = matrix.length;
        int c = matrix[0].length;
        int col0 = 1;
        for (int i = 0; i < r; i++) {
            if (matrix[i][0] == 0) col0 = 0;
            for (int j = 1; j < c; j++)
                if (matrix[i][j] == 0)
                    matrix[i][0] = matrix[0][j] = 0;
        }

        for (int i = r - 1; i >= 0; i--) {
            for (int j = c - 1; j >= 1; j--)
                if (matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;
            if (col0 == 0) matrix[i][0] = 0;
        }
    }
}

Solution in Python Programming

class Solution:
    def setZeroes(self, matrix):
        """
        Do not return anything, modify matrix in-place instead.
        """
        row = []
        col = []
        
        for r in range(0,len(matrix)):
            for c in range(0,len(matrix[0])):
                if matrix[r][c] == 0:
                    row.append(r)
                    col.append(c)
                    
        for a in row:
            for b in range(0,len(matrix[0])):
                matrix[a][b] = 0
                
        for i in col:
            for j in range(0,len(matrix)):
                matrix[j][i] = 0

Solution in C# Programming

public class Solution {
    public void SetZeroes(int[][] matrix) {
        var r = new Dictionary<int,int>();
        var c = new Dictionary<int,int>();
        
        for(int i=0;i<matrix.Length;i++){
            for(int j=0;j<matrix[0].Length;j++){
                if(matrix[i][j]==0){
                    if(!r.ContainsKey(i)){
                        r.Add(i,0);
                    }
                    if(!c.ContainsKey(j)){
                        c.Add(j,0);
                    }
                }
            }
        }
        
        foreach(var i in r.Keys){
            RMakeZero(i,matrix);
        }
        foreach(var i in c.Keys){
            CMakeZero(i,matrix);
        }
    }
    public static void RMakeZero(int row,int[][] matrix){
        for(int i=0;i<matrix[0].Length;i++){
            matrix[row][i] =0;
        }
    }
    public static void CMakeZero(int col,int[][] matrix){
        
        for(int i=0;i<matrix.Length;i++){
            matrix[i][col] =0;
        }
    }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *