forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_73.java
134 lines (124 loc) · 3.93 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
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;
}
}
}
}
}