Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

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.

Leetcode 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];
        }
    }
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes