Skip to content

Commit 9f4cd92

Browse files
committed
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+
}

0 commit comments

Comments
 (0)