-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetMatrixZeros.java
66 lines (54 loc) · 1.76 KB
/
SetMatrixZeros.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
*Key point: I once conceived a O(m+n) space solution, but the
* best is a constant space solution. That's using first row and
* first column as the indicator array to store which column and row
* should be set to 0. Two boolean variables are needed to store the
* state of first row and column
*/
public class Solution {
public void setZeroes(int[][] matrix) {
if(matrix == null)
return;
int m = matrix.length;
int n = matrix[0].length;
boolean firstRowHasZero = false;
boolean firstColHasZero = false;
int i,j;
//checking first row
for(j = 0;j<n;j++){
if(matrix[0][j] == 0)
firstRowHasZero = true;
}
//checking first column
for(i = 0;i<m;i++){
if(matrix[i][0] == 0)
firstColHasZero = true;
}
//checking the rest of matrix and setting corresponding indicator
for(i = 1;i<m;i++){
for(j = 1;j<n;j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
//setting to 0 according to indicator
for(i=1;i<m;i++){
for(j = 1;j<n;j++){
if(matrix[0][j] == 0 || matrix[i][0] == 0)
matrix[i][j] = 0;
}
}
//handle the first row itself
if(firstRowHasZero == true){
for(j = 0;j<n;j++)
matrix[0][j] = 0;
}
//handle the first column itself
if(firstColHasZero == true){
for(i = 0;i<m;i++)
matrix[i][0] = 0;
}
}
}