Skip to content

Commit 4ce8747

Browse files
committed
feat: add ts solution to lc problem: No.0959
1 parent d3e53ba commit 4ce8747

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

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

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

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

284335
```js

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

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

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

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

0 commit comments

Comments
 (0)