Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Set Matrix Zeroes problem solution

YASH PAL, 31 July 202418 January 2026

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

Leetcode Set Matrix Zeroes 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

Set Matrix Zeroes 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 solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes