Skip to content

Commit 638b468

Browse files
committed
0073. Set Matrix Zeroes
1 parent a61ce03 commit 638b468

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

markdown/0073. Set Matrix Zeroes.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
### [73\. Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a _m_ x _n_ matrix, if an element is 0, set its entire row and column to 0\. Do it .
7+
8+
**Example 1:**
9+
10+
```
11+
Input:
12+
[
13+
  [1,1,1],
14+
  [1,0,1],
15+
  [1,1,1]
16+
]
17+
Output:
18+
[
19+
  [1,0,1],
20+
  [0,0,0],
21+
  [1,0,1]
22+
]
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input:
29+
[
30+
  [0,1,2,0],
31+
  [3,4,5,2],
32+
  [1,3,1,5]
33+
]
34+
Output:
35+
[
36+
  [0,0,0,0],
37+
  [0,4,5,0],
38+
  [0,3,1,0]
39+
]
40+
```
41+
42+
**Follow up:**
43+
44+
* A straight forward solution using O(_m__n_) space is probably a bad idea.
45+
* A simple improvement uses O(_m_ + _n_) space, but still not the best solution.
46+
* Could you devise a constant space solution?
47+
48+
49+
#### Solution
50+
51+
Language: **Java**
52+
53+
```java
54+
class Solution {
55+
   public void setZeroes(int[][] matrix) {
56+
       int row0 = 1;
57+
       for (int i = 0; i < matrix.length; i++) {
58+
           for (int j = 0; j < matrix[0].length; j++) {
59+
               if (matrix[i][j] == 0) {
60+
                   if (i == 0) {
61+
                       row0 = 0;
62+
                  } else {
63+
                       matrix[i][0] = 0;
64+
                  }
65+
                   matrix[0][j] = 0; // 表示第 j 列
66+
              }
67+
          }
68+
      }
69+
       for (int i = matrix.length - 1; i >= 1; i--) {
70+
           for (int j = matrix[0].length - 1; j >= 0; j--) {
71+
               if (matrix[0][j] == 0 || matrix[i][0] == 0) {
72+
                   matrix[i][j] = 0;
73+
              }
74+
          }
75+
      }
76+
       if (row0 == 0) {
77+
           for (int i = 0; i < matrix[0].length; i++) {
78+
               matrix[0][i] = 0;
79+
          }
80+
      }
81+
  }
82+
}
83+
```
84+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190722143115.png)

src/main/java/leetcode/_73_/Main.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode._73_;
2+
3+
import leetcode.common.Printer;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
// int[][] matrix = {
12+
// {1, 1, 1},
13+
// {1, 0, 1},
14+
// {1, 1, 1}};
15+
int[][] martix = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
16+
Printer.printArrays(martix);
17+
solution.setZeroes(martix);
18+
Printer.printArrays(martix);
19+
20+
21+
int[][] martix2 = {{1,0,3}};
22+
solution.setZeroes(martix2);
23+
Printer.printArrays(martix2);
24+
}
25+
}
26+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode._73_;
2+
3+
class Solution {
4+
public void setZeroes(int[][] matrix) {
5+
int row0 = 1;
6+
for (int i = 0; i < matrix.length; i++) {
7+
for (int j = 0; j < matrix[0].length; j++) {
8+
if (matrix[i][j] == 0) {
9+
if (i == 0) {
10+
row0 = 0;
11+
} else {
12+
matrix[i][0] = 0;
13+
}
14+
matrix[0][j] = 0; // 表示第 j 列
15+
}
16+
}
17+
}
18+
for (int i = matrix.length - 1; i >= 1; i--) {
19+
for (int j = matrix[0].length - 1; j >= 0; j--) {
20+
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
21+
matrix[i][j] = 0;
22+
}
23+
}
24+
}
25+
if (row0 == 0) {
26+
for (int i = 0; i < matrix[0].length; i++) {
27+
matrix[0][i] = 0;
28+
}
29+
}
30+
}
31+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
### [73\. Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a _m_ x _n_ matrix, if an element is 0, set its entire row and column to 0\. Do it .
7+
8+
**Example 1:**
9+
10+
```
11+
Input:
12+
[
13+
  [1,1,1],
14+
  [1,0,1],
15+
  [1,1,1]
16+
]
17+
Output:
18+
[
19+
  [1,0,1],
20+
  [0,0,0],
21+
  [1,0,1]
22+
]
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input:
29+
[
30+
  [0,1,2,0],
31+
  [3,4,5,2],
32+
  [1,3,1,5]
33+
]
34+
Output:
35+
[
36+
  [0,0,0,0],
37+
  [0,4,5,0],
38+
  [0,3,1,0]
39+
]
40+
```
41+
42+
**Follow up:**
43+
44+
* A straight forward solution using O(_m__n_) space is probably a bad idea.
45+
* A simple improvement uses O(_m_ + _n_) space, but still not the best solution.
46+
* Could you devise a constant space solution?
47+
48+
49+
#### Solution
50+
51+
Language: **Java**
52+
53+
```java
54+
class Solution {
55+
   public void setZeroes(int[][] matrix) {
56+
       int row0 = 1;
57+
       for (int i = 0; i < matrix.length; i++) {
58+
           for (int j = 0; j < matrix[0].length; j++) {
59+
               if (matrix[i][j] == 0) {
60+
                   if (i == 0) {
61+
                       row0 = 0;
62+
                  } else {
63+
                       matrix[i][0] = 0;
64+
                  }
65+
                   matrix[0][j] = 0; // 表示第 j 列
66+
              }
67+
          }
68+
      }
69+
       for (int i = matrix.length - 1; i >= 1; i--) {
70+
           for (int j = matrix[0].length - 1; j >= 0; j--) {
71+
               if (matrix[0][j] == 0 || matrix[i][0] == 0) {
72+
                   matrix[i][j] = 0;
73+
              }
74+
          }
75+
      }
76+
       if (row0 == 0) {
77+
           for (int i = 0; i < matrix[0].length; i++) {
78+
               matrix[0][i] = 0;
79+
          }
80+
      }
81+
  }
82+
}
83+
```
84+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190722143115.png)

src/main/java/leetcode/common/Printer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,23 @@ public static void printArrays(int[] arrays) {
1515
}
1616
System.out.print(arrays[arrays.length - 1] + "]");
1717
}
18+
19+
public static void printArrays(int[][] arrays) {
20+
if (arrays == null) {
21+
System.out.println("[]");
22+
return;
23+
}
24+
System.out.print("[\n");
25+
for (int i = 0; i < arrays.length; i++) {
26+
System.out.print("\t[");
27+
for (int j = 0; j < arrays[0].length; j++) {
28+
System.out.print(arrays[i][j]);
29+
if (j != arrays[0].length - 1) {
30+
System.out.print(",");
31+
}
32+
}
33+
System.out.println("]");
34+
}
35+
System.out.println("]");
36+
}
1837
}

0 commit comments

Comments
 (0)