Skip to content

Commit 3c4e032

Browse files
committed
feat: set-matrix-zeroes
1 parent 94a952d commit 3c4e032

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

set-matrix-zeroes/minji-go.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/set-matrix-zeroes/">week7-5.set-matrix-zeroes</a>
3+
* <li>Description: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's</li>
4+
* <li>Topics: Array, Hash Table, Matrix </li>
5+
* <li>Time Complexity: O(MN), Runtime 0ms </li>
6+
* <li>Space Complexity: O(M+N), Memory 45.58MB </li>
7+
*/
8+
class Solution {
9+
public void setZeroes(int[][] matrix) {
10+
int m = matrix.length;
11+
int n = matrix[0].length;
12+
13+
boolean[] rowZero = new boolean[m];
14+
boolean[] colZero = new boolean[n];
15+
16+
for (int i = 0; i < m; i++) {
17+
for (int j = 0; j < n; j++) {
18+
if (matrix[i][j] == 0) {
19+
rowZero[i] = true;
20+
colZero[j] = true;
21+
}
22+
}
23+
}
24+
25+
for (int i = 0; i < m; i++) {
26+
if (rowZero[i]) {
27+
for (int j = 0; j < n; j++) {
28+
matrix[i][j] = 0;
29+
}
30+
}
31+
}
32+
for (int i = 0; i < n; i++) {
33+
if (colZero[i]) {
34+
for (int j = 0; j < m; j++) {
35+
matrix[j][i] = 0;
36+
}
37+
}
38+
}
39+
40+
}
41+
}

0 commit comments

Comments
 (0)