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 f47bcdb

Browse files
authored
refactor: update ts solution to lc problem: No.323 (doocs#2992)
1 parent 88a131a commit f47bcdb

File tree

3 files changed

+42
-48
lines changed

3 files changed

+42
-48
lines changed

‎solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -643,29 +643,27 @@ func countComponents(n int, edges [][]int) (ans int) {
643643

644644
```ts
645645
function countComponents(n: number, edges: number[][]): number {
646-
const g: number[][] = Array.from({ length: n }, () => []);
646+
const g: Map<number, number[]> = newMap(Array.from({ length: n }, (_, i) => [i, []]));
647647
for (const [a, b] of edges) {
648-
g[a].push(b);
649-
g[b].push(a);
648+
g.get(a)!.push(b);
649+
g.get(b)!.push(a);
650650
}
651-
const vis: boolean[] = Array(n).fill(false);
651+
652+
const vis = new Set<number>();
652653
let ans = 0;
653-
for (let i =0; i<n; ++i) {
654-
if (vis[i]) {
654+
for (const [i] ofg) {
655+
if (vis.has(i)) {
655656
continue;
656657
}
657-
vis[i] = true;
658-
++ans;
659-
const q: number[] = [i];
660-
while (q.length) {
661-
const a = q.pop()!;
662-
for (const b of g[a]) {
663-
if (!vis[b]) {
664-
vis[b] = true;
665-
q.push(b);
666-
}
658+
const q = [i];
659+
for (const j of q) {
660+
if (vis.has(j)) {
661+
continue;
667662
}
663+
vis.add(j);
664+
q.push(...g.get(j)!);
668665
}
666+
ans++;
669667
}
670668
return ans;
671669
}

‎solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -638,29 +638,27 @@ func countComponents(n int, edges [][]int) (ans int) {
638638

639639
```ts
640640
function countComponents(n: number, edges: number[][]): number {
641-
const g: number[][] = Array.from({ length: n }, () => []);
641+
const g: Map<number, number[]> = newMap(Array.from({ length: n }, (_, i) => [i, []]));
642642
for (const [a, b] of edges) {
643-
g[a].push(b);
644-
g[b].push(a);
643+
g.get(a)!.push(b);
644+
g.get(b)!.push(a);
645645
}
646-
const vis: boolean[] = Array(n).fill(false);
646+
647+
const vis = new Set<number>();
647648
let ans = 0;
648-
for (let i =0; i<n; ++i) {
649-
if (vis[i]) {
649+
for (const [i] ofg) {
650+
if (vis.has(i)) {
650651
continue;
651652
}
652-
vis[i] = true;
653-
++ans;
654-
const q: number[] = [i];
655-
while (q.length) {
656-
const a = q.pop()!;
657-
for (const b of g[a]) {
658-
if (!vis[b]) {
659-
vis[b] = true;
660-
q.push(b);
661-
}
653+
const q = [i];
654+
for (const j of q) {
655+
if (vis.has(j)) {
656+
continue;
662657
}
658+
vis.add(j);
659+
q.push(...g.get(j)!);
663660
}
661+
ans++;
664662
}
665663
return ans;
666664
}
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
function countComponents(n: number, edges: number[][]): number {
2-
const g: number[][] = Array.from({ length: n }, () => []);
2+
const g: Map<number,number[]> = newMap(Array.from({ length: n }, (_,i) => [i,[]]));
33
for (const [a, b] of edges) {
4-
g[a].push(b);
5-
g[b].push(a);
4+
g.get(a)!.push(b);
5+
g.get(b)!.push(a);
66
}
7-
const vis: boolean[] = Array(n).fill(false);
7+
8+
const vis = new Set<number>();
89
let ans = 0;
9-
for (leti=0;i<n;++i) {
10-
if (vis[i]) {
10+
for (const[i]ofg) {
11+
if (vis.has(i)) {
1112
continue;
1213
}
13-
vis[i] = true;
14-
++ans;
15-
const q: number[] = [i];
16-
while (q.length) {
17-
const a = q.pop()!;
18-
for (const b of g[a]) {
19-
if (!vis[b]) {
20-
vis[b] = true;
21-
q.push(b);
22-
}
14+
const q = [i];
15+
for (const j of q) {
16+
if (vis.has(j)) {
17+
continue;
2318
}
19+
vis.add(j);
20+
q.push(...g.get(j)!);
2421
}
22+
ans++;
2523
}
2624
return ans;
2725
}

0 commit comments

Comments
(0)

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