Skip to content

Commit 86489f4

Browse files
committed
add solution of problem 73: set matrix zeroes
1 parent 0224d94 commit 86489f4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

SetMatrixZeroes73/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given a *m* x *n* matrix, if an element is 0, set its entire row and column to 0. Do it in place.

SetMatrixZeroes73/Solution.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution {
2+
class Index {
3+
public final int first;
4+
public final int second;
5+
public Index(int first, int second) {
6+
this.first = first;
7+
this.second = second;
8+
}
9+
}
10+
11+
public void setZeroes(int[][] matrix) {
12+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
13+
return;
14+
15+
List<Index> indexList = new ArrayList<Index>();
16+
for (int i = 0; i < matrix.length; i++) {
17+
for (int j = 0; j < matrix[i].length; j++) {
18+
if (matrix[i][j] == 0)
19+
indexList.add(new Index(i, j));
20+
}
21+
}
22+
23+
for (Index idx : indexList) {
24+
for (int i = 0; i < matrix[0].length; i++)
25+
matrix[idx.first][i] = 0;
26+
27+
for (int i = 0; i < matrix.length; i++)
28+
matrix[i][idx.second] = 0;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)