Table of Contents

# Set Matrix Zeroes – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

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`

`-2`

^{31}<= matrix[i][j] <= 2^{31}- 1

# C++ Set Matrix Zeroes LeetCode Solution

```
``````
void setZeroes(vector<vector<int>>& a) {
int n = a.size();
int m = a[0].size();
bool firstRow = false; // do we need to set first row zero
bool firstCol = false; // do we need to ser first col zero
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(a[i][j] == 0){ // store rows and cols state in first row and col
if(i==0) firstRow = true;
if(j==0) firstCol = true;
a[i][0] = 0;
a[0][j] = 0;
}
}
}
// cout<<firstRow<<" "<<firstCol<<endl;
for(int i=1; i<n; i++){
for(int j=1; j<m; j++){
if(a[i][0] == 0 || a[0][j] == 0){
a[i][j] = 0;
}
}
}
if(firstRow){
for(int i=0;i<m;i++) a[0][i] = 0;
}
if(firstCol){
for(int i=0;i<n;i++) a[i][0] = 0;
}
}
```

# Java Set Matrix Zeroes LeetCode Solution

```
``````
public class Solution {
public void setZeroes(int[][] matrix) {
boolean fr = false,fc = false;
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) fr = true;
if(j == 0) fc = true;
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(fr) {
for(int j = 0; j < matrix[0].length; j++) {
matrix[0][j] = 0;
}
}
if(fc) {
for(int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
}
```

}

# Python 3 Set Matrix Zeroes LeetCode Solution

```
``````
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
# input validation
if not matrix:
return []
m = len(matrix)
n = len(matrix[0])
zeroes_row = [False] * m
zeroes_col = [False] * n
for row in range(m):
for col in range(n):
if matrix[row][col] == 0:
zeroes_row[row] = True
zeroes_col[col] = True
for row in range(m):
for col in range(n):
if zeroes_row[row] or zeroes_col[col]:
matrix[row][col] = 0
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1

## Leave a comment below