-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_set_zero.java
More file actions
27 lines (24 loc) · 839 Bytes
/
Matrix_set_zero.java
File metadata and controls
27 lines (24 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Matrix_set_zero {
public void setZeroes(int[][] matrix) {
boolean zeroinFirstCol = false;
for (int row = 0; row < matrix.length; row++) {
if (matrix[row][0] == 0) zeroinFirstCol = true;
for (int col = 1; col < matrix[0].length; col++) {
if (matrix[row][col] == 0) {
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
}
for (int row = matrix.length - 1; row >= 0; row--) {
for (int col = matrix[0].length - 1; col >= 1; col--) {
if (matrix[row][0] == 0 || matrix[0][col] == 0) {
matrix[row][col] = 0;
}
}
if (zeroinFirstCol) {
matrix[row][0] = 0;
}
}
}
}