Leetcode Set Matrix Zeroes problem solution YASH PAL, 31 July 2024 In this Leetcode Set Matrix Zeroes problem solution we have Given an m x n integer matrix, if an element is 0, set its entire row and column to 0’s, and return the matrix. 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]; } } } coding problems