Skip to content

Commit 7c5ba68

Browse files
73.set matrix zeroes (#72)
* Added 73. Set Matrix Zeroes (Java) * Updated Readme Added 73. SetMatrixZeroes Java
1 parent 534de6c commit 7c5ba68

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Java/set-matrix-zeroes.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
Boolean isCol = false;
4+
int R = matrix.length, C = matrix[0].length;
5+
for (int i = 0; i < R; i++) {
6+
if (matrix[i][0] == 0) {
7+
isCol = true;
8+
}
9+
for (int j = 1; j < C; j++) {
10+
if (matrix[i][j] == 0) {
11+
matrix[0][j] = 0;
12+
matrix[i][0] = 0;
13+
}
14+
}
15+
}
16+
for (int i = 1; i < R; i++) {
17+
for (int j = 1; j < C; j++) {
18+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
19+
matrix[i][j] = 0;
20+
}
21+
}
22+
}
23+
if (matrix[0][0] == 0) {
24+
for (int j = 0; j < C; j++) {
25+
matrix[0][j] = 0;
26+
}
27+
}
28+
if (isCol) {
29+
for (int i = 0; i < R; i++) {
30+
matrix[i][0] = 0;
31+
}
32+
}
33+
}
34+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
105105
| 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/) | [Python](./python/SmallestDifference.py) | O(n) | O(1) | Easy | Array |
106106
| 532 | [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [C++](./C++/k-diff-pairs-in-an-array.cpp) | O(n) | O(n) | Medium | Array |
107107
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Javascript](./JavaScript/152.Maximum-Product-Subarray.js) | O(n) | O(n) | Medium | Array |
108+
| 073 | [Set-Matrix-Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Java](./Java/set-matrix-zeroes.java) | O(MN) | O(1) | Medium | Array |
108109

109110

110111
<br/>

0 commit comments

Comments
 (0)