-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path73. Set Matrix Zeroes.java
64 lines (55 loc) · 1.75 KB
/
73. Set Matrix Zeroes.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
class Solution {
public void setZeroes(int[][] matrix) {
int R = matrix.length;
int C = matrix[0].length;
boolean isColumnZero = checkColIfZero(matrix, R, C);
boolean isRowZero = checkRowIfZero(matrix, R, C);
for (int row = 1; row < R; row++) {
for (int col = 1; col < C; col++) {
int elem = matrix[row][col];
if (elem == 0) {
matrix[0][col] = 0;
matrix[row][0] = 0;
}
}
}
for (int col = 1; col < C; col++) {
if (matrix[0][col]==0) {
for (int row = 0; row < R; row++) {
matrix[row][col] = 0;
}
}
}
for (int row = 1; row < R; row++) {
if (matrix[row][0]==0) {
for (int col = 0; col < C; col++) {
matrix[row][col] = 0;
}
}
}
if (isColumnZero) {
for (int row = 0 ; row < R; row++) {
matrix[row][0] = 0;
}
}
if (isRowZero) {
for (int col = 0; col < C; col++) {
matrix[0][col] = 0;
}
}
}
private boolean checkRowIfZero(int[][] matrix, int r, int c) {
// TODO Auto-generated method stub
for (int col = 0; col < c; col++)
if (matrix[0][col] == 0) return true;
return false;
}
private boolean checkColIfZero(int[][] matrix, int r, int c) {
// TODO Auto-generated method stub
for (int row = 0; row < r; row++)
if (matrix[row][0]==0) return true;
return false;
}
}