Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8e78c4a

Browse files
refactor 73
1 parent 58db0cd commit 8e78c4a

File tree

3 files changed

+194
-16
lines changed

3 files changed

+194
-16
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ Your ideas/fixes/algorithms are more than welcome!
534534
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_76.java)|O(n)|O(k)|Hard|Two Pointers
535535
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_75.java)|O(n)|O(1)|Medium| Two Pointers
536536
|74|[Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_74.java)|O(log(m*n))|O(1)|Medium| Binary Search
537-
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_73.java)|O(mn)|O(mn)|Medium|
537+
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_73.java)|O(mn)|O(1)|Medium|
538538
|72|[Edit Distance](https://leetcode.com/problems/edit-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_72.java)|O(m*n)|O(m+n)|Hard|
539539
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_71.java)|O(n)|O(n)|Medium| Stack
540540
|70|[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_70.java)|O(n)|O(n)|Easy| DP
+110-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

3-
/**Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
3+
/**
4+
* 73. Set Matrix Zeroes
5+
*
6+
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
47
58
Follow up:
69
Did you use extra space?
@@ -10,25 +13,117 @@ A simple improvement uses O(m + n) space, but still not the best solution.
1013
1114
*/
1215
public class _73 {
13-
//this is the most straightforward solution which uses O(mn) space
14-
public void setZeroes(int[][] matrix) {
15-
if(matrix == null || matrix.length == 0) return;
16-
int height = matrix.length, width = matrix[0].length;
17-
boolean[][] zero = new boolean[height][width];
18-
for(int i = 0; i < height; i++){
19-
for(int j = 0; j < width; j++){
20-
if(matrix[i][j] == 0) zero[i][j] = true;
16+
17+
public static class Solution1 {
18+
/**
19+
* Space: O(m*n)
20+
*/
21+
public void setZeroes(int[][] matrix) {
22+
if (matrix == null || matrix.length == 0) return;
23+
int height = matrix.length, width = matrix[0].length;
24+
boolean[][] zero = new boolean[height][width];
25+
for (int i = 0; i < height; i++) {
26+
for (int j = 0; j < width; j++) {
27+
if (matrix[i][j] == 0) {
28+
zero[i][j] = true;
29+
}
30+
}
31+
}
32+
for (int i = 0; i < height; i++) {
33+
for (int j = 0; j < width; j++) {
34+
if (zero[i][j]) {
35+
for (int k = 0; k < height; k++) {
36+
matrix[k][j] = 0;
37+
}
38+
for (int k = 0; k < width; k++) {
39+
matrix[i][k] = 0;
40+
}
41+
}
42+
}
2143
}
2244
}
23-
for(int i = 0; i < height; i++){
24-
for(int j = 0; j < width; j++){
25-
if(zero[i][j]){
26-
for(int k = 0; k < height; k++) matrix[k][j] = 0;
27-
for(int k = 0; k < width; k++) matrix[i][k] = 0;
45+
}
46+
47+
public static class Solution2 {
48+
/**
49+
* Space: O(m+n)
50+
*/
51+
public void setZeroes(int[][] matrix) {
52+
if (matrix == null || matrix.length == 0) {
53+
return;
54+
}
55+
int m = matrix.length;
56+
int n = matrix[0].length;
57+
boolean[] row = new boolean[m];
58+
boolean[] col = new boolean[n];
59+
for (int i = 0; i < m; i++) {
60+
for (int j = 0; j < n; j++) {
61+
if (matrix[i][j] == 0) {
62+
row[i] = true;
63+
col[j] = true;
64+
}
65+
}
66+
}
67+
68+
for (int i = 0; i < m; i++) {
69+
for (int j = 0; j < n; j++) {
70+
if (row[i] && col[j]) {
71+
for (int k = 0; k < m; k++) {
72+
matrix[k][j] = 0;
73+
}
74+
for (int k = 0; k < n; k++) {
75+
matrix[i][k] = 0;
76+
}
77+
}
2878
}
2979
}
3080
}
3181
}
3282

33-
//TODO: use better solutions
83+
public static class Solution3 {
84+
/**
85+
* Space: O(1)
86+
*/
87+
public void setZeroes(int[][] matrix) {
88+
if (matrix == null || matrix.length == 0) return;
89+
int m = matrix.length;
90+
int n = matrix[0].length;
91+
boolean firstRow = false;
92+
boolean firstCol = false;
93+
for (int i = 0; i < m; i++) {
94+
for (int j = 0; j < n; j++) {
95+
if (matrix[i][j] == 0) {
96+
if (i == 0) {
97+
firstRow = true;
98+
}
99+
if (j == 0) {
100+
firstCol = true;
101+
}
102+
matrix[i][0] = 0;
103+
matrix[0][j] = 0;
104+
}
105+
}
106+
}
107+
108+
for (int i = 1; i < m; i++) {
109+
for (int j = 1; j < n; j++) {
110+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
111+
matrix[i][j] = 0;
112+
}
113+
}
114+
}
115+
116+
if (firstRow) {
117+
for (int j = 0; j < n; j++) {
118+
matrix[0][j] = 0;
119+
}
120+
}
121+
122+
if (firstCol) {
123+
for (int i = 0; i < m; i++) {
124+
matrix[i][0] = 0;
125+
}
126+
}
127+
}
128+
}
34129
}
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._73;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _73Test {
10+
private static _73.Solution1 solution1;
11+
private static _73.Solution2 solution2;
12+
private static _73.Solution3 solution3;
13+
private static int[][] matrix;
14+
private static int[][] expected;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
solution1 = new _73.Solution1();
19+
solution2 = new _73.Solution2();
20+
solution3 = new _73.Solution3();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
matrix = new int[][]{
26+
{0,0,0,5},
27+
{4,3,1,4},
28+
{0,1,1,4},
29+
{1,2,1,3},
30+
{0,0,1,1}
31+
};
32+
solution1.setZeroes(matrix);
33+
expected = new int[][]{
34+
{0,0,0,0},
35+
{0,0,0,4},
36+
{0,0,0,0},
37+
{0,0,0,3},
38+
{0,0,0,0}
39+
};
40+
assertArrayEquals(expected, matrix);
41+
}
42+
43+
@Test
44+
public void test2(){
45+
matrix = new int[][]{
46+
{0,0,0,5},
47+
{4,3,1,4},
48+
{0,1,1,4},
49+
{1,2,1,3},
50+
{0,0,1,1}
51+
};
52+
solution2.setZeroes(matrix);
53+
expected = new int[][]{
54+
{0,0,0,0},
55+
{0,0,0,4},
56+
{0,0,0,0},
57+
{0,0,0,3},
58+
{0,0,0,0}
59+
};
60+
assertArrayEquals(expected, matrix);
61+
}
62+
63+
@Test
64+
public void test3(){
65+
matrix = new int[][]{
66+
{0,0,0,5},
67+
{4,3,1,4},
68+
{0,1,1,4},
69+
{1,2,1,3},
70+
{0,0,1,1}
71+
};
72+
solution3.setZeroes(matrix);
73+
expected = new int[][]{
74+
{0,0,0,0},
75+
{0,0,0,4},
76+
{0,0,0,0},
77+
{0,0,0,3},
78+
{0,0,0,0}
79+
};
80+
assertArrayEquals(expected, matrix);
81+
}
82+
83+
}

0 commit comments

Comments
 (0)