Set Matrix Zeroes LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct]

LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++

Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the Set Matrix Zeroes in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

About LeetCode

LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews. 

LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either EasyMedium, or Hard.

LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:

  • Mathematics/Basic Logical Based Questions
  • Arrays
  • Strings
  • Hash Table
  • Dynamic Programming
  • Stack & Queue
  • Trees & Graphs
  • Greedy Algorithms
  • Breadth-First Search
  • Depth-First Search
  • Sorting & Searching
  • BST (Binary Search Tree)
  • Database
  • Linked List
  • Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

Link for the ProblemSet Matrix Zeroes– LeetCode Problem

Set Matrix Zeroes – LeetCode Problem

Problem:

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0‘s, and return the matrix.

You must do it in place.

Example 1:

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

Example 2:

mat2
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
Set Matrix Zeroes– LeetCode Solutions
Set Matrix Zeroes in C++:
class Solution {
 public:
  void setZeroes(vector<vector<int>>& matrix) {
    const int m = matrix.size();
    const int n = matrix[0].size();

    bool shouldFillFirstRow = false;
    bool shouldFillFirstCol = false;

    for (int j = 0; j < n; ++j)
      if (matrix[0][j] == 0) {
        shouldFillFirstRow = true;
        break;
      }

    for (int i = 0; i < m; ++i)
      if (matrix[i][0] == 0) {
        shouldFillFirstCol = true;
        break;
      }

    // store the information in the 1st row/col
    for (int i = 1; i < m; ++i)
      for (int j = 1; j < n; ++j)
        if (matrix[i][j] == 0) {
          matrix[i][0] = 0;
          matrix[0][j] = 0;
        }

    // fill 0s for the matrix except the 1st row/col
    for (int i = 1; i < m; ++i)
      for (int j = 1; j < n; ++j)
        if (matrix[i][0] == 0 || matrix[0][j] == 0)
          matrix[i][j] = 0;

    // fill 0s for the 1st row if needed
    if (shouldFillFirstRow)
      for (int j = 0; j < n; ++j)
        matrix[0][j] = 0;

    // fill 0s for the 1st col if needed
    if (shouldFillFirstCol)
      for (int i = 0; i < m; ++i)
        matrix[i][0] = 0;
  }
};
Set Matrix Zeroes in Java:
class Solution {
  public void setZeroes(int[][] matrix) {
    final int m = matrix.length;
    final int n = matrix[0].length;

    boolean shouldFillFirstRow = false;
    boolean shouldFillFirstCol = false;

    for (int j = 0; j < n; ++j)
      if (matrix[0][j] == 0) {
        shouldFillFirstRow = true;
        break;
      }

    for (int i = 0; i < m; ++i)
      if (matrix[i][0] == 0) {
        shouldFillFirstCol = true;
        break;
      }

    // store the information in the 1st row/col
    for (int i = 1; i < m; ++i)
      for (int j = 1; j < n; ++j)
        if (matrix[i][j] == 0) {
          matrix[i][0] = 0;
          matrix[0][j] = 0;
        }

    // fill 0s for the matrix except the 1st row/col
    for (int i = 1; i < m; ++i)
      for (int j = 1; j < n; ++j)
        if (matrix[i][0] == 0 || matrix[0][j] == 0)
          matrix[i][j] = 0;

    // fill 0s for the 1st row if needed
    if (shouldFillFirstRow)
      for (int j = 0; j < n; ++j)
        matrix[0][j] = 0;

    // fill 0s for the 1st col if needed
    if (shouldFillFirstCol)
      for (int i = 0; i < m; ++i)
        matrix[i][0] = 0;
  }
}
Set Matrix Zeroes in Python:
class Solution:
  def setZeroes(self, matrix: List[List[int]]) -> None:
    m = len(matrix)
    n = len(matrix[0])

    shouldFillFirstRow = 0 in matrix[0]
    shouldFillFirstCol = 0 in list(zip(*matrix))[0]

    # store the information in the 1st row/col
    for i in range(1, m):
      for j in range(1, n):
        if matrix[i][j] == 0:
          matrix[i][0] = 0
          matrix[0][j] = 0

    # fill 0s for the matrix except the 1st row/col
    for i in range(1, m):
      for j in range(1, n):
        if matrix[i][0] == 0 or matrix[0][j] == 0:
          matrix[i][j] = 0

    # fill 0s for the 1st row if needed
    if shouldFillFirstRow:
      matrix[0] = [0] * n

    # fill 0s for the 1st col if needed
    if shouldFillFirstCol:
      for row in matrix:
        row[0] = 0

123 thoughts on “Set Matrix Zeroes LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct]”

  1. I like the valuable info you provide on your articles.
    I will bookmark your blog and check again here frequently.
    I’m moderately certain I will learn plenty of
    new stuff proper right here! Good luck for the
    following!

    Reply
  2. I must thank you for the efforts you’ve put in writing this site.
    I am hoping to see the same high-grade blog posts from
    you in the future as well. In fact, your creative
    writing abilities has encouraged me to get my very
    own site now 😉

    Reply
  3. This is really interesting, You’re an overly professional
    blogger. I’ve joined your feed and look forward to in the hunt for extra of
    your wonderful post. Also, I have shared your website in my
    social networks

    Reply
  4. Thanks for one’s marvelous posting! I certainly enjoyed reading it, you could be a
    great author. I will remember to bookmark your blog and will come back at some point.

    I want to encourage that you continue your great work, have a
    nice morning!

    Reply
  5. Wonderful blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Appreciate it

    Reply
  6. Someone necessarily lend a hand to make significantly posts I would
    state. That is the first time I frequented your
    web page and thus far? I surprised with the analysis you made to create this actual submit extraordinary.
    Great process!

    Reply
  7. Hello would you mind letting me know which web host you’re utilizing?
    I’ve loaded your blog in 3 completely different internet browsers
    and I must say this blog loads a lot faster then most.

    Can you suggest a good web hosting provider at a honest price?
    Kudos, I appreciate it!

    Reply
  8. Can I simply just say what a relief to find someone who actually
    understands what they are talking about on the internet.
    You certainly know how to bring a problem to light and make it important.
    More people must look at this and understand this side of your story.

    I was surprised you aren’t more popular given that you surely possess
    the gift.

    Reply
  9. Thank you for the auspicious writeup. It if truth be told was a enjoyment account it.

    Look advanced to far added agreeable from you! By the way, how can we keep up a correspondence?

    Reply
  10. Hi there! This is kind of off topic but I need some help from an established
    blog. Is it very difficult to set up your own blog? I’m not very
    techincal but I can figure things out pretty fast.
    I’m thinking about making my own but I’m not sure where
    to start. Do you have any tips or suggestions?
    Many thanks

    Reply
  11. Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but
    I never seem to get there! Thank you

    Reply
  12. Hmm is anyone else experiencing problems with the images on this blog loading?
    I’m trying to figure out if its a problem on my end or if
    it’s the blog. Any suggestions would be greatly appreciated.

    Reply
  13. Hi, i read your blog occasionally and i own a similar one and i was
    just curious if you get a lot of spam feedback?
    If so how do you prevent it, any plugin or anything you can recommend?

    I get so much lately it’s driving me insane so any support is very
    much appreciated.

    Reply
  14. Great post. I was checking continuously this blog and I’m impressed!
    Extremely useful information particularly the last part 🙂 I care for such information a lot.
    I was seeking this certain info for a long time.
    Thank you and best of luck.

    Reply
  15. Greetings from California! I’m bored to death
    at work so I decided to browse your site on my iphone during
    lunch break. I really like the info you
    present here and can’t wait to take a look when I get home.
    I’m shocked at how fast your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyhow, great blog!

    Reply
  16. Let me give you a thumbs up man. Can I tell you my secret ways
    on amazing values and if you want to have a checkout and also
    share valuable info about how to learn SNS marketing
    yalla lready know follow me my fellow commenters!.

    Reply

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock