Skip to content

Commit 9f4cd92

Browse files
committedApr 22, 2023
feat: add solutions to lc problem: No.0073
No.0073.Set Matrix Zeroes
1 parent 32ca260 commit 9f4cd92

File tree

9 files changed

+456
-77
lines changed

9 files changed

+456
-77
lines changed
 

‎solution/0000-0099/0073.Set Matrix Zeroes/README.md

+130
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,136 @@ var setZeroes = function (matrix) {
404404
};
405405
```
406406

407+
### **TypeScript**
408+
409+
```ts
410+
/**
411+
Do not return anything, modify matrix in-place instead.
412+
*/
413+
function setZeroes(matrix: number[][]): void {
414+
const m = matrix.length;
415+
const n = matrix[0].length;
416+
const rows: boolean[] = new Array(m).fill(false);
417+
const cols: boolean[] = new Array(n).fill(false);
418+
for (let i = 0; i < m; ++i) {
419+
for (let j = 0; j < n; ++j) {
420+
if (matrix[i][j] === 0) {
421+
rows[i] = true;
422+
cols[j] = true;
423+
}
424+
}
425+
}
426+
for (let i = 0; i < m; ++i) {
427+
for (let j = 0; j < n; ++j) {
428+
if (rows[i] || cols[j]) {
429+
matrix[i][j] = 0;
430+
}
431+
}
432+
}
433+
}
434+
```
435+
436+
```ts
437+
/**
438+
Do not return anything, modify matrix in-place instead.
439+
*/
440+
function setZeroes(matrix: number[][]): void {
441+
const m = matrix.length;
442+
const n = matrix[0].length;
443+
const i0 = matrix[0].includes(0);
444+
const j0 = matrix.map(row => row[0]).includes(0);
445+
for (let i = 1; i < m; ++i) {
446+
for (let j = 1; j < n; ++j) {
447+
if (matrix[i][j] === 0) {
448+
matrix[i][0] = 0;
449+
matrix[0][j] = 0;
450+
}
451+
}
452+
}
453+
for (let i = 1; i < m; ++i) {
454+
for (let j = 1; j < n; ++j) {
455+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
456+
matrix[i][j] = 0;
457+
}
458+
}
459+
}
460+
if (i0) {
461+
matrix[0].fill(0);
462+
}
463+
if (j0) {
464+
for (let i = 0; i < m; ++i) {
465+
matrix[i][0] = 0;
466+
}
467+
}
468+
}
469+
```
470+
471+
### **C#**
472+
473+
```cs
474+
public class Solution {
475+
public void SetZeroes(int[][] matrix) {
476+
int m = matrix.Length, n = matrix[0].Length;
477+
bool[] rows = new bool[m], cols = new bool[n];
478+
for (int i = 0; i < m; ++i) {
479+
for (int j = 0; j < n; ++j) {
480+
if (matrix[i][j] == 0) {
481+
rows[i] = true;
482+
cols[j] = true;
483+
}
484+
}
485+
}
486+
for (int i = 0; i < m; ++i) {
487+
for (int j = 0; j < n; ++j) {
488+
if (rows[i] || cols[j]) {
489+
matrix[i][j] = 0;
490+
}
491+
}
492+
}
493+
}
494+
}
495+
```
496+
497+
```cs
498+
public class Solution {
499+
public void SetZeroes(int[][] matrix) {
500+
int m = matrix.Length, n = matrix[0].Length;
501+
bool i0 = matrix[0].Contains(0), j0 = false;
502+
for (int i = 0; i < m; ++i) {
503+
if (matrix[i][0] == 0) {
504+
j0 = true;
505+
break;
506+
}
507+
}
508+
for (int i = 1; i < m; ++i) {
509+
for (int j = 1; j < n; ++j) {
510+
if (matrix[i][j] == 0) {
511+
matrix[i][0] = 0;
512+
matrix[0][j] = 0;
513+
}
514+
}
515+
}
516+
for (int i = 1; i < m; ++i) {
517+
for (int j = 1; j < n; ++j) {
518+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
519+
matrix[i][j] = 0;
520+
}
521+
}
522+
}
523+
if (i0) {
524+
for (int j = 0; j < n; ++j) {
525+
matrix[0][j] = 0;
526+
}
527+
}
528+
if (j0) {
529+
for (int i = 0; i < m; ++i) {
530+
matrix[i][0] = 0;
531+
}
532+
}
533+
}
534+
}
535+
```
536+
407537
### **...**
408538

409539
```

‎solution/0000-0099/0073.Set Matrix Zeroes/README_EN.md

+146
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@
4444

4545
## Solutions
4646

47+
**Approach 1: Array Mark**
48+
49+
We use arrays `rows` and `cols` to mark the rows and columns to be cleared.
50+
51+
Then traverse the matrix again, and clear the elements in the rows and columns marked in `rows` and `cols`.
52+
53+
The time complexity is $O(m\times n)$, and the space complexity is $O(m+n)$. Where $m$ and $n$ are the number of rows and columns of the matrix respectively.
54+
55+
**Approach 2: Mark in Place**
56+
57+
In the first method, we use an additional array to mark the rows and columns to be cleared. In fact, we can also use the first row and first column of the matrix to mark them, without creating an additional array.
58+
59+
Since the first row and the first column are used to mark, their values ​​may change due to the mark, so we need additional variables $i0$, $j0$ to mark whether the first row and the first column need to be cleared.
60+
61+
The time complexity is $O(m\times n)$, and the space complexity is $O(1)$. Where $m$ and $n$ are the number of rows and columns of the matrix respectively.
62+
4763
<!-- tabs:start -->
4864

4965
### **Python3**
@@ -376,6 +392,136 @@ var setZeroes = function (matrix) {
376392
};
377393
```
378394

395+
### **TypeScript**
396+
397+
```ts
398+
/**
399+
Do not return anything, modify matrix in-place instead.
400+
*/
401+
function setZeroes(matrix: number[][]): void {
402+
const m = matrix.length;
403+
const n = matrix[0].length;
404+
const rows: boolean[] = new Array(m).fill(false);
405+
const cols: boolean[] = new Array(n).fill(false);
406+
for (let i = 0; i < m; ++i) {
407+
for (let j = 0; j < n; ++j) {
408+
if (matrix[i][j] === 0) {
409+
rows[i] = true;
410+
cols[j] = true;
411+
}
412+
}
413+
}
414+
for (let i = 0; i < m; ++i) {
415+
for (let j = 0; j < n; ++j) {
416+
if (rows[i] || cols[j]) {
417+
matrix[i][j] = 0;
418+
}
419+
}
420+
}
421+
}
422+
```
423+
424+
```ts
425+
/**
426+
Do not return anything, modify matrix in-place instead.
427+
*/
428+
function setZeroes(matrix: number[][]): void {
429+
const m = matrix.length;
430+
const n = matrix[0].length;
431+
const i0 = matrix[0].includes(0);
432+
const j0 = matrix.map(row => row[0]).includes(0);
433+
for (let i = 1; i < m; ++i) {
434+
for (let j = 1; j < n; ++j) {
435+
if (matrix[i][j] === 0) {
436+
matrix[i][0] = 0;
437+
matrix[0][j] = 0;
438+
}
439+
}
440+
}
441+
for (let i = 1; i < m; ++i) {
442+
for (let j = 1; j < n; ++j) {
443+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
444+
matrix[i][j] = 0;
445+
}
446+
}
447+
}
448+
if (i0) {
449+
matrix[0].fill(0);
450+
}
451+
if (j0) {
452+
for (let i = 0; i < m; ++i) {
453+
matrix[i][0] = 0;
454+
}
455+
}
456+
}
457+
```
458+
459+
### **C#**
460+
461+
```cs
462+
public class Solution {
463+
public void SetZeroes(int[][] matrix) {
464+
int m = matrix.Length, n = matrix[0].Length;
465+
bool[] rows = new bool[m], cols = new bool[n];
466+
for (int i = 0; i < m; ++i) {
467+
for (int j = 0; j < n; ++j) {
468+
if (matrix[i][j] == 0) {
469+
rows[i] = true;
470+
cols[j] = true;
471+
}
472+
}
473+
}
474+
for (int i = 0; i < m; ++i) {
475+
for (int j = 0; j < n; ++j) {
476+
if (rows[i] || cols[j]) {
477+
matrix[i][j] = 0;
478+
}
479+
}
480+
}
481+
}
482+
}
483+
```
484+
485+
```cs
486+
public class Solution {
487+
public void SetZeroes(int[][] matrix) {
488+
int m = matrix.Length, n = matrix[0].Length;
489+
bool i0 = matrix[0].Contains(0), j0 = false;
490+
for (int i = 0; i < m; ++i) {
491+
if (matrix[i][0] == 0) {
492+
j0 = true;
493+
break;
494+
}
495+
}
496+
for (int i = 1; i < m; ++i) {
497+
for (int j = 1; j < n; ++j) {
498+
if (matrix[i][j] == 0) {
499+
matrix[i][0] = 0;
500+
matrix[0][j] = 0;
501+
}
502+
}
503+
}
504+
for (int i = 1; i < m; ++i) {
505+
for (int j = 1; j < n; ++j) {
506+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
507+
matrix[i][j] = 0;
508+
}
509+
}
510+
}
511+
if (i0) {
512+
for (int j = 0; j < n; ++j) {
513+
matrix[0][j] = 0;
514+
}
515+
}
516+
if (j0) {
517+
for (int i = 0; i < m; ++i) {
518+
matrix[i][0] = 0;
519+
}
520+
}
521+
}
522+
}
523+
```
524+
379525
### **...**
380526

381527
```

‎solution/0000-0099/0073.Set Matrix Zeroes/Solution.cpp

+30-9
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,43 @@ class Solution {
22
public:
33
void setZeroes(vector<vector<int>>& matrix) {
44
int m = matrix.size(), n = matrix[0].size();
5-
vector<bool> rows(m);
6-
vector<bool> cols(n);
5+
bool i0 = false, j0 = false;
6+
for (int j = 0; j < n; ++j) {
7+
if (matrix[0][j] == 0) {
8+
i0 = true;
9+
break;
10+
}
11+
}
712
for (int i = 0; i < m; ++i) {
8-
for (int j = 0; j < n; ++j) {
9-
if (!matrix[i][j]) {
10-
rows[i] = 1;
11-
cols[j] = 1;
13+
if (matrix[i][0] == 0) {
14+
j0 = true;
15+
break;
16+
}
17+
}
18+
for (int i = 1; i < m; ++i) {
19+
for (int j = 1; j < n; ++j) {
20+
if (matrix[i][j] == 0) {
21+
matrix[i][0] = 0;
22+
matrix[0][j] = 0;
1223
}
1324
}
1425
}
15-
for (int i = 0; i < m; ++i) {
16-
for (int j = 0; j < n; ++j) {
17-
if (rows[i] || cols[j]) {
26+
for (int i = 1; i < m; ++i) {
27+
for (int j = 1; j < n; ++j) {
28+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
1829
matrix[i][j] = 0;
1930
}
2031
}
2132
}
33+
if (i0) {
34+
for (int j = 0; j < n; ++j) {
35+
matrix[0][j] = 0;
36+
}
37+
}
38+
if (j0) {
39+
for (int i = 0; i < m; ++i) {
40+
matrix[i][0] = 0;
41+
}
42+
}
2243
}
2344
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Solution {
2+
public void SetZeroes(int[][] matrix) {
3+
int m = matrix.Length, n = matrix[0].Length;
4+
bool i0 = matrix[0].Contains(0), j0 = false;
5+
for (int i = 0; i < m; ++i) {
6+
if (matrix[i][0] == 0) {
7+
j0 = true;
8+
break;
9+
}
10+
}
11+
for (int i = 1; i < m; ++i) {
12+
for (int j = 1; j < n; ++j) {
13+
if (matrix[i][j] == 0) {
14+
matrix[i][0] = 0;
15+
matrix[0][j] = 0;
16+
}
17+
}
18+
}
19+
for (int i = 1; i < m; ++i) {
20+
for (int j = 1; j < n; ++j) {
21+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
22+
matrix[i][j] = 0;
23+
}
24+
}
25+
}
26+
if (i0) {
27+
for (int j = 0; j < n; ++j) {
28+
matrix[0][j] = 0;
29+
}
30+
}
31+
if (j0) {
32+
for (int i = 0; i < m; ++i) {
33+
matrix[i][0] = 0;
34+
}
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
func setZeroes(matrix [][]int) {
22
m, n := len(matrix), len(matrix[0])
3-
rows := make([]bool, m)
4-
cols := make([]bool, n)
5-
for i, row := range matrix {
6-
for j, v := range row {
7-
if v == 0 {
8-
rows[i] = true
9-
cols[j] = true
10-
}
3+
i0, j0 := false, false
4+
for j := 0; j < n; j++ {
5+
if matrix[0][j] == 0 {
6+
i0 = true
7+
break
118
}
129
}
1310
for i := 0; i < m; i++ {
14-
for j := 0; j < n; j++ {
15-
if rows[i] || cols[j] {
11+
if matrix[i][0] == 0 {
12+
j0 = true
13+
break
14+
}
15+
}
16+
for i := 1; i < m; i++ {
17+
for j := 1; j < n; j++ {
18+
if matrix[i][j] == 0 {
19+
matrix[i][0], matrix[0][j] = 0, 0
20+
}
21+
}
22+
}
23+
for i := 1; i < m; i++ {
24+
for j := 1; j < n; j++ {
25+
if matrix[i][0] == 0 || matrix[0][j] == 0 {
1626
matrix[i][j] = 0
1727
}
1828
}
1929
}
30+
if i0 {
31+
for j := 0; j < n; j++ {
32+
matrix[0][j] = 0
33+
}
34+
}
35+
if j0 {
36+
for i := 0; i < m; i++ {
37+
matrix[i][0] = 0
38+
}
39+
}
2040
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
class Solution {
22
public void setZeroes(int[][] matrix) {
33
int m = matrix.length, n = matrix[0].length;
4-
boolean[] rows = new boolean[m];
5-
boolean[] cols = new boolean[n];
4+
boolean i0 = false, j0 = false;
5+
for (int j = 0; j < n; ++j) {
6+
if (matrix[0][j] == 0) {
7+
i0 = true;
8+
break;
9+
}
10+
}
611
for (int i = 0; i < m; ++i) {
7-
for (int j = 0; j < n; ++j) {
12+
if (matrix[i][0] == 0) {
13+
j0 = true;
14+
break;
15+
}
16+
}
17+
for (int i = 1; i < m; ++i) {
18+
for (int j = 1; j < n; ++j) {
819
if (matrix[i][j] == 0) {
9-
rows[i] = true;
10-
cols[j] = true;
20+
matrix[i][0] = 0;
21+
matrix[0][j] = 0;
1122
}
1223
}
1324
}
14-
for (int i = 0; i < m; ++i) {
15-
for (int j = 0; j < n; ++j) {
16-
if (rows[i] || cols[j]) {
25+
for (int i = 1; i < m; ++i) {
26+
for (int j = 1; j < n; ++j) {
27+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
1728
matrix[i][j] = 0;
1829
}
1930
}
2031
}
32+
if (i0) {
33+
for (int j = 0; j < n; ++j) {
34+
matrix[0][j] = 0;
35+
}
36+
}
37+
if (j0) {
38+
for (int i = 0; i < m; ++i) {
39+
matrix[i][0] = 0;
40+
}
41+
}
2142
}
2243
}

‎solution/0000-0099/0073.Set Matrix Zeroes/Solution.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,37 @@
55
var setZeroes = function (matrix) {
66
const m = matrix.length;
77
const n = matrix[0].length;
8-
const rows = new Array(m).fill(false);
9-
const cols = new Array(n).fill(false);
8+
let i0 = matrix[0].some(v => v == 0);
9+
let j0 = false;
1010
for (let i = 0; i < m; ++i) {
11-
for (let j = 0; j < n; ++j) {
11+
if (matrix[i][0] == 0) {
12+
j0 = true;
13+
break;
14+
}
15+
}
16+
for (let i = 1; i < m; ++i) {
17+
for (let j = 1; j < n; ++j) {
1218
if (matrix[i][j] == 0) {
13-
rows[i] = true;
14-
cols[j] = true;
19+
matrix[i][0] = 0;
20+
matrix[0][j] = 0;
1521
}
1622
}
1723
}
18-
for (let i = 0; i < m; ++i) {
19-
for (let j = 0; j < n; ++j) {
20-
if (rows[i] || cols[j]) {
24+
for (let i = 1; i < m; ++i) {
25+
for (let j = 1; j < n; ++j) {
26+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
2127
matrix[i][j] = 0;
2228
}
2329
}
2430
}
31+
if (i0) {
32+
for (let j = 0; j < n; ++j) {
33+
matrix[0][j] = 0;
34+
}
35+
}
36+
if (j0) {
37+
for (let i = 0; i < m; ++i) {
38+
matrix[i][0] = 0;
39+
}
40+
}
2541
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
class Solution:
22
def setZeroes(self, matrix: List[List[int]]) -> None:
33
m, n = len(matrix), len(matrix[0])
4-
rows = [0] * m
5-
cols = [0] * n
6-
for i, row in enumerate(matrix):
7-
for j, v in enumerate(row):
8-
if v == 0:
9-
rows[i] = cols[j] = 1
10-
for i in range(m):
11-
for j in range(n):
12-
if rows[i] or cols[j]:
4+
i0 = any(v == 0 for v in matrix[0])
5+
j0 = any(matrix[i][0] == 0 for i in range(m))
6+
for i in range(1, m):
7+
for j in range(1, n):
8+
if matrix[i][j] == 0:
9+
matrix[i][0] = matrix[0][j] = 0
10+
for i in range(1, m):
11+
for j in range(1, n):
12+
if matrix[i][0] == 0 or matrix[0][j] == 0:
1313
matrix[i][j] = 0
14+
if i0:
15+
for j in range(n):
16+
matrix[0][j] = 0
17+
if j0:
18+
for i in range(m):
19+
matrix[i][0] = 0

‎solution/0000-0099/0073.Set Matrix Zeroes/Solution.ts

+15-33
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,31 @@
22
Do not return anything, modify matrix in-place instead.
33
*/
44
function setZeroes(matrix: number[][]): void {
5-
let m = matrix.length,
6-
n = matrix[0].length;
7-
let c0 = false,
8-
r0 = false;
9-
// 遍历第一行
10-
for (let i = 0; i < m; i++) {
11-
if (!matrix[i][0] && !c0) {
12-
c0 = true;
13-
}
14-
}
15-
// 第一列
16-
for (let j = 0; j < n; j++) {
17-
if (!matrix[0][j] && !r0) {
18-
r0 = true;
19-
}
20-
}
21-
// 用第一行、第一列标记全部
22-
for (let i = 1; i < m; i++) {
23-
for (let j = 1; j < n; j++) {
24-
if (!matrix[i][j]) {
5+
const m = matrix.length;
6+
const n = matrix[0].length;
7+
const i0 = matrix[0].includes(0);
8+
const j0 = matrix.map(row => row[0]).includes(0);
9+
for (let i = 1; i < m; ++i) {
10+
for (let j = 1; j < n; ++j) {
11+
if (matrix[i][j] === 0) {
2512
matrix[i][0] = 0;
2613
matrix[0][j] = 0;
2714
}
2815
}
2916
}
30-
// set
31-
for (let i = 1; i < m; i++) {
32-
for (let j = 1; j < n; j++) {
33-
if (!matrix[i][0] || !matrix[0][j]) {
17+
for (let i = 1; i < m; ++i) {
18+
for (let j = 1; j < n; ++j) {
19+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
3420
matrix[i][j] = 0;
3521
}
3622
}
3723
}
38-
// set 第一列
39-
if (c0) {
40-
for (let i = 0; i < m; i++) {
41-
matrix[i][0] = 0;
42-
}
24+
if (i0) {
25+
matrix[0].fill(0);
4326
}
44-
// 第一行
45-
if (r0) {
46-
for (let j = 0; j < n; j++) {
47-
matrix[0][j] = 0;
27+
if (j0) {
28+
for (let i = 0; i < m; ++i) {
29+
matrix[i][0] = 0;
4830
}
4931
}
5032
}

0 commit comments

Comments
 (0)
Please sign in to comment.