forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetZeroes.java
More file actions
139 lines (117 loc) · 3.76 KB
/
SetZeroes.java
File metadata and controls
139 lines (117 loc) · 3.76 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package Algorithms.array;
public class SetZeroes {
public static void main(String[] strs) {
int[][] matrix = {
{0,0,0,5},{4,3,1,4},{0,1,1,4},{1,2,1,3},{0,0,1,1}
};
setZeroes(matrix);
}
public static void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0
|| matrix[0].length == 0) {
return;
}
boolean row1Zero = false;
boolean col1Zero = false;
int rows = matrix.length;
int cols = matrix[0].length;
// Determine if the first column should be Zero.
for (int i = 0; i < rows; i++) {
if (matrix[i][0] == 0) {
col1Zero = true;
break;
}
}
// Determine if the first row should be Zero.
for (int i = 0; i < cols; i++) {
if (matrix[0][i] == 0) {
row1Zero = true;
break;
}
}
// we use the first row and the first col as the flag to record the
// cells whether or not set to 0.
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i][j] == 0) {
// set the flag in the first line and the first column
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
// set the inner cells.
// Be careful: i, j start from 1.
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i][0] == 0
|| matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
// set the first row.
if (row1Zero) {
for (int i = 0; i < cols; i++) {
matrix[0][i] = 0;
}
}
// set the first col.
if (col1Zero) {
for (int i = 0; i < rows; i++) {
matrix[i][0] = 0;
}
}
return;
}
// solution 2: combine the first 3 loop.
public void setZeroes2(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
boolean row1 = false;
boolean col1 = false;
int rows = matrix.length;
int cols = matrix[0].length;
// set flags.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
continue;
}
// set the flag of a column and a row.
matrix[0][j] = 0;
matrix[i][0] = 0;
// get flag of first row.
if (i == 0) {
row1 = true;
}
// get flag of first column.
if (j == 0) {
col1 = true;
}
}
}
// set the matrix.
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}
// set first column
if (col1) {
for (int i = 0; i < rows; i++) {
// bug 1: can't use matrix[i][j]
matrix[i][0] = 0;
}
}
// set first row.
if (row1) {
for (int i = 0; i < cols; i++) {
matrix[0][i] = 0;
}
}
}
}