Skip to content

Commit d36635f

Browse files
authored
feat: add javascript solution to lcci problem: No.01.08.Zero Matrix (#372)
* feat: add javascript solution to lcci problem: No.02.01.Remove Duplicate Node * feat: add javascript solution to lcci problem: No.02.02.Kth Node From End of List * feat: add javascript solution to lcci problem: No.02.03.Delete Middle Node * feat: add javascript solution to lcci problem: No.17.04.Missing Number * feat: add javascript solution to lcci problem: No.17.10.Find Majority Element * feat: add javascript solution to lcci problem: No.01.08.Zero Matrix
1 parent e41ebf5 commit d36635f

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Diff for: lcci/01.08.Zero Matrix/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,37 @@ class Solution {
117117
}
118118
```
119119

120+
### **JavaScript**
121+
122+
```js
123+
/**
124+
* @param {number[][]} matrix
125+
* @return {void} Do not return anything, modify matrix in-place instead.
126+
*/
127+
var setZeroes = function(matrix) {
128+
let m = matrix.length, n = matrix[0].length;
129+
let rows = new Array(m).fill(false);
130+
let cols = new Array(n).fill(false);
131+
// 标记
132+
for (let i = 0; i < m; i++) {
133+
for (let j = 0; j < n; j++) {
134+
if (matrix[i][j] == 0) {
135+
rows[i] = true;
136+
cols[j] = true;
137+
}
138+
}
139+
}
140+
// 清零
141+
for (let i = 0; i < m; i++) {
142+
for (let j = 0; j < n; j++) {
143+
if (matrix[i][j] != 0 && (rows[i] || cols[j])) {
144+
matrix[i][j] = 0;
145+
}
146+
}
147+
}
148+
};
149+
```
150+
120151
### **...**
121152

122153
```

Diff for: lcci/01.08.Zero Matrix/README_EN.md

+31
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,37 @@ class Solution {
132132
}
133133
```
134134

135+
### **JavaScript**
136+
137+
```js
138+
/**
139+
* @param {number[][]} matrix
140+
* @return {void} Do not return anything, modify matrix in-place instead.
141+
*/
142+
var setZeroes = function(matrix) {
143+
let m = matrix.length, n = matrix[0].length;
144+
let rows = new Array(m).fill(false);
145+
let cols = new Array(n).fill(false);
146+
// 标记
147+
for (let i = 0; i < m; i++) {
148+
for (let j = 0; j < n; j++) {
149+
if (matrix[i][j] == 0) {
150+
rows[i] = true;
151+
cols[j] = true;
152+
}
153+
}
154+
}
155+
// 清零
156+
for (let i = 0; i < m; i++) {
157+
for (let j = 0; j < n; j++) {
158+
if (matrix[i][j] != 0 && (rows[i] || cols[j])) {
159+
matrix[i][j] = 0;
160+
}
161+
}
162+
}
163+
};
164+
```
165+
135166
### **...**
136167

137168
```

Diff for: lcci/01.08.Zero Matrix/Solution.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
var setZeroes = function(matrix) {
6+
let m = matrix.length, n = matrix[0].length;
7+
let rows = new Array(m).fill(false);
8+
let cols = new Array(n).fill(false);
9+
// 标记
10+
for (let i = 0; i < m; i++) {
11+
for (let j = 0; j < n; j++) {
12+
if (matrix[i][j] == 0) {
13+
rows[i] = true;
14+
cols[j] = true;
15+
}
16+
}
17+
}
18+
// 清零
19+
for (let i = 0; i < m; i++) {
20+
for (let j = 0; j < n; j++) {
21+
if (matrix[i][j] != 0 && (rows[i] || cols[j])) {
22+
matrix[i][j] = 0;
23+
}
24+
}
25+
}
26+
};

0 commit comments

Comments
 (0)