Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 794b63a

Browse files
feat: add js solution to lc problem: No.0959 (#3396)
1 parent 6df3dbf commit 794b63a

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

‎solution/0900-0999/0959.Regions Cut By Slashes/README.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,62 @@ func regionsBySlashes(grid []string) int {
279279
}
280280
```
281281

282+
#### JavaScript
283+
284+
```js
285+
/**
286+
* @param {string[]} grid
287+
* @return {number}
288+
*/
289+
290+
function regionsBySlashes(grid) {
291+
const find = x => {
292+
if (p[x] !== x) {
293+
p[x] = find(p[x]);
294+
}
295+
return p[x];
296+
};
297+
298+
const union = (a, b) => {
299+
const pa = find(a);
300+
const pb = find(b);
301+
if (pa !== pb) {
302+
p[pa] = pb;
303+
size--;
304+
}
305+
};
306+
307+
const n = grid.length;
308+
let size = n * n * 4;
309+
const p = Array.from({ length: size }, (_, i) => i);
310+
311+
for (let i = 0; i < n; i++) {
312+
for (let j = 0; j < n; j++) {
313+
const k = i * n + j;
314+
if (i < n - 1) {
315+
union(4 * k + 2, (k + n) * 4);
316+
}
317+
if (j < n - 1) {
318+
union(4 * k + 1, (k + 1) * 4 + 3);
319+
}
320+
if (grid[i][j] === '/') {
321+
union(4 * k, 4 * k + 3);
322+
union(4 * k + 1, 4 * k + 2);
323+
} else if (grid[i][j] === '\\') {
324+
union(4 * k, 4 * k + 1);
325+
union(4 * k + 2, 4 * k + 3);
326+
} else {
327+
union(4 * k, 4 * k + 1);
328+
union(4 * k + 1, 4 * k + 2);
329+
union(4 * k + 2, 4 * k + 3);
330+
}
331+
}
332+
}
333+
334+
return size;
335+
}
336+
```
337+
282338
<!-- tabs:end -->
283339

284340
<!-- solution:end -->

‎solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,62 @@ func regionsBySlashes(grid []string) int {
269269
}
270270
```
271271

272+
#### JavaScript
273+
274+
```js
275+
/**
276+
* @param {string[]} grid
277+
* @return {number}
278+
*/
279+
280+
function regionsBySlashes(grid) {
281+
const find = x => {
282+
if (p[x] !== x) {
283+
p[x] = find(p[x]);
284+
}
285+
return p[x];
286+
};
287+
288+
const union = (a, b) => {
289+
const pa = find(a);
290+
const pb = find(b);
291+
if (pa !== pb) {
292+
p[pa] = pb;
293+
size--;
294+
}
295+
};
296+
297+
const n = grid.length;
298+
let size = n * n * 4;
299+
const p = Array.from({ length: size }, (_, i) => i);
300+
301+
for (let i = 0; i < n; i++) {
302+
for (let j = 0; j < n; j++) {
303+
const k = i * n + j;
304+
if (i < n - 1) {
305+
union(4 * k + 2, (k + n) * 4);
306+
}
307+
if (j < n - 1) {
308+
union(4 * k + 1, (k + 1) * 4 + 3);
309+
}
310+
if (grid[i][j] === '/') {
311+
union(4 * k, 4 * k + 3);
312+
union(4 * k + 1, 4 * k + 2);
313+
} else if (grid[i][j] === '\\') {
314+
union(4 * k, 4 * k + 1);
315+
union(4 * k + 2, 4 * k + 3);
316+
} else {
317+
union(4 * k, 4 * k + 1);
318+
union(4 * k + 1, 4 * k + 2);
319+
union(4 * k + 2, 4 * k + 3);
320+
}
321+
}
322+
}
323+
324+
return size;
325+
}
326+
```
327+
272328
<!-- tabs:end -->
273329

274330
<!-- solution:end -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @param {string[]} grid
3+
* @return {number}
4+
*/
5+
6+
function regionsBySlashes(grid) {
7+
const find = x => {
8+
if (p[x] !== x) {
9+
p[x] = find(p[x]);
10+
}
11+
return p[x];
12+
};
13+
14+
const union = (a, b) => {
15+
const pa = find(a);
16+
const pb = find(b);
17+
if (pa !== pb) {
18+
p[pa] = pb;
19+
size--;
20+
}
21+
};
22+
23+
const n = grid.length;
24+
let size = n * n * 4;
25+
const p = Array.from({ length: size }, (_, i) => i);
26+
27+
for (let i = 0; i < n; i++) {
28+
for (let j = 0; j < n; j++) {
29+
const k = i * n + j;
30+
if (i < n - 1) {
31+
union(4 * k + 2, (k + n) * 4);
32+
}
33+
if (j < n - 1) {
34+
union(4 * k + 1, (k + 1) * 4 + 3);
35+
}
36+
if (grid[i][j] === '/') {
37+
union(4 * k, 4 * k + 3);
38+
union(4 * k + 1, 4 * k + 2);
39+
} else if (grid[i][j] === '\\') {
40+
union(4 * k, 4 * k + 1);
41+
union(4 * k + 2, 4 * k + 3);
42+
} else {
43+
union(4 * k, 4 * k + 1);
44+
union(4 * k + 1, 4 * k + 2);
45+
union(4 * k + 2, 4 * k + 3);
46+
}
47+
}
48+
}
49+
50+
return size;
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /