forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_73.java
176 lines (163 loc) · 5.65 KB
/
_73.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.fishercoder.solutions;
/**
* 73. Set Matrix Zeroes
*
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
*/
public class _73 {
public static class Solution1 {
/**
* Space: O(m*n)
*/
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
return;
}
int height = matrix.length;
int width = matrix[0].length;
boolean[][] zero = new boolean[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (matrix[i][j] == 0) {
zero[i][j] = true;
}
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (zero[i][j]) {
for (int k = 0; k < height; k++) {
matrix[k][j] = 0;
}
for (int k = 0; k < width; k++) {
matrix[i][k] = 0;
}
}
}
}
}
}
public static class Solution2 {
/**
* Space: O(m+n)
*/
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
return;
}
int m = matrix.length;
int n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
row[i] = true;
col[j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] && col[j]) {
for (int k = 0; k < m; k++) {
matrix[k][j] = 0;
}
for (int k = 0; k < n; k++) {
matrix[i][k] = 0;
}
}
}
}
}
}
public static class Solution3 {
/**
* Space: O(1)
*/
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
return;
}
int m = matrix.length;
int n = matrix[0].length;
boolean firstRow = false;
boolean firstCol = false;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
if (i == 0) {
firstRow = true;
}
if (j == 0) {
firstCol = true;
}
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if (firstRow) {
for (int j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
if (firstCol) {
for (int i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
}
}
public static class Solution4 {
/**
* Space: O(1)
* credit: https://leetcode.com/problems/set-matrix-zeroes/discuss/26014/Any-shorter-O(1)-space-solution
*/
public void setZeroes(int[][] matrix) {
int col0 = 1;
int m = matrix.length;
int n = matrix[0].length;
/**the first iteration (first nested for loop) is to check from top row to bottom row:
* keep the first column state into variable col0;
* then starting from the second column, check all the rest of the columns and mark its top cell and its most-left cell if it
* s a zero.*/
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
col0 = 0;
}
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
/**the second iteration (second nested for loop) is to check from bottom row to the top row
* from the right-most column to the second left-most column: as long as its left-most column cell or its top row cell is zero, then set that cell to be zero
* at last, check col0 variable, if it's zero, mark that row cell as zero*/
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 1; j--) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
if (col0 == 0) {
matrix[i][0] = 0;
}
}
}
}
}