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 770693e

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.1971 (doocs#2900)
* feat: ✨ add TS solution to lc problem: No.1971 * Update README_EN.md * Update README.md * Update Solution.py * Update Solution.java * Update Solution.cpp * Update Solution.ts * Update Solution2.py * Update Solution2.java * Update Solution2.cpp * Update Solution2.go * Update Solution2.ts * Create Solution2.rs * Create Solution3.py * Create Solution3.java * Create Solution3.cpp * Create Solution3.go * Create Solution3.ts --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent d9494c8 commit 770693e

File tree

17 files changed

+841
-207
lines changed

17 files changed

+841
-207
lines changed

‎solution/1900-1999/1971.Find if Path Exists in Graph/README.md‎

Lines changed: 274 additions & 119 deletions
Large diffs are not rendered by default.

‎solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md‎

Lines changed: 308 additions & 30 deletions
Large diffs are not rendered by default.

‎solution/1900-1999/1971.Find if Path Exists in Graph/Solution.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ class Solution {
22
public:
33
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
44
vector<bool> vis(n);
5-
vector<vector<int>> g(n);
5+
vector<int> g[n];
66
for (auto& e : edges) {
77
int a = e[0], b = e[1];
88
g[a].emplace_back(b);
99
g[b].emplace_back(a);
1010
}
1111
function<bool(int)> dfs = [&](int i) -> bool {
12-
if (i == destination) return true;
12+
if (i == destination) {
13+
return true;
14+
}
1315
vis[i] = true;
1416
for (int& j : g[i]) {
1517
if (!vis[j] && dfs(j)) {
@@ -20,4 +22,4 @@ class Solution {
2022
};
2123
return dfs(source);
2224
}
23-
};
25+
};
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
class Solution {
2+
private int destination;
23
private boolean[] vis;
34
private List<Integer>[] g;
45

56
public boolean validPath(int n, int[][] edges, int source, int destination) {
6-
vis = new boolean[n];
77
g = new List[n];
88
Arrays.setAll(g, k -> new ArrayList<>());
99
for (var e : edges) {
1010
int a = e[0], b = e[1];
1111
g[a].add(b);
1212
g[b].add(a);
1313
}
14-
return dfs(source, destination);
14+
vis = new boolean[n];
15+
this.destination = destination;
16+
return dfs(source);
1517
}
1618

17-
private boolean dfs(int source, intdestination) {
18-
if (source == destination) {
19+
private boolean dfs(int i) {
20+
if (i == destination) {
1921
return true;
2022
}
21-
vis[source] = true;
22-
for (int nxt : g[source]) {
23-
if (!vis[nxt] && dfs(nxt, destination)) {
23+
vis[i] = true;
24+
for (int j : g[i]) {
25+
if (!vis[j] && dfs(j)) {
2426
return true;
2527
}
2628
}
2729
return false;
2830
}
29-
}
31+
}

‎solution/1900-1999/1971.Find if Path Exists in Graph/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def dfs(i):
1111
return True
1212
return False
1313

14-
g = defaultdict(list)
14+
g = [[] for_inrange(n)]
1515
for a, b in edges:
1616
g[a].append(b)
1717
g[b].append(a)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
2+
const g: number[][] = Array.from({ length: n }, () => []);
3+
for (const [a, b] of edges) {
4+
g[a].push(b);
5+
g[b].push(a);
6+
}
7+
8+
const vis = new Set<number>();
9+
const dfs = (i: number) => {
10+
if (i === destination) {
11+
return true;
12+
}
13+
if (vis.has(i)) {
14+
return false;
15+
}
16+
17+
vis.add(i);
18+
return g[i].some(dfs);
19+
};
20+
21+
return dfs(source);
22+
}
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
class Solution {
22
public:
33
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
4-
vector<int> p(n);
5-
iota(p.begin(), p.end(), 0);
6-
function<int(int)> find = [&](int x) -> int {
7-
if (p[x] != x) p[x] = find(p[x]);
8-
return p[x];
9-
};
10-
for (auto& e : edges) p[find(e[0])] = find(e[1]);
11-
return find(source) == find(destination);
4+
vector<vector<int>> g(n);
5+
for (auto& e : edges) {
6+
int a = e[0], b = e[1];
7+
g[a].push_back(b);
8+
g[b].push_back(a);
9+
}
10+
queue<int> q{{source}};
11+
vector<bool> vis(n);
12+
vis[source] = true;
13+
while (q.size()) {
14+
int i = q.front();
15+
q.pop();
16+
if (i == destination) {
17+
return true;
18+
}
19+
for (int j : g[i]) {
20+
if (!vis[j]) {
21+
vis[j] = true;
22+
q.push(j);
23+
}
24+
}
25+
}
26+
return false;
1227
}
13-
};
28+
};
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
func validPath(n int, edges [][]int, source int, destination int) bool {
2-
p := make([]int, n)
3-
for i := range p {
4-
p[i] = i
2+
g := make([][]int, n)
3+
for _, e := range edges {
4+
a, b := e[0], e[1]
5+
g[a] = append(g[a], b)
6+
g[b] = append(g[b], a)
57
}
6-
var find func(x int) int
7-
find = func(x int) int {
8-
if p[x] != x {
9-
p[x] = find(p[x])
8+
q := []int{source}
9+
vis := make([]bool, n)
10+
vis[source] = true
11+
for len(q) > 0 {
12+
i := q[0]
13+
q = q[1:]
14+
if i == destination {
15+
return true
16+
}
17+
for _, j := range g[i] {
18+
if !vis[j] {
19+
vis[j] = true
20+
q = append(q, j)
21+
}
1022
}
11-
return p[x]
12-
}
13-
for _, e := range edges {
14-
p[find(e[0])] = find(e[1])
1523
}
16-
return find(source) ==find(destination)
17-
}
24+
return false
25+
}
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
class Solution {
2-
private int[] p;
3-
42
public boolean validPath(int n, int[][] edges, int source, int destination) {
5-
p = new int[n];
6-
for (int i = 0; i < n; ++i) {
7-
p[i] = i;
3+
List<Integer>[] g = new List[n];
4+
Arrays.setAll(g, k -> new ArrayList<>());
5+
for (var e : edges) {
6+
int a = e[0], b = e[1];
7+
g[a].add(b);
8+
g[b].add(a);
89
}
9-
for (int[] e : edges) {
10-
p[find(e[0])] = find(e[1]);
10+
Deque<Integer> q = new ArrayDeque<>();
11+
q.offer(source);
12+
boolean[] vis = new boolean[n];
13+
vis[source] = true;
14+
while (!q.isEmpty()) {
15+
int i = q.poll();
16+
if (i == destination) {
17+
return true;
18+
}
19+
for (int j : g[i]) {
20+
if (!vis[j]) {
21+
vis[j] = true;
22+
q.offer(j);
23+
}
24+
}
1125
}
12-
return find(source) == find(destination);
26+
return false;
1327
}
14-
15-
private int find(int x) {
16-
if (p[x] != x) {
17-
p[x] = find(p[x]);
18-
}
19-
return p[x];
20-
}
21-
}
28+
}

‎solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.py‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ class Solution:
22
def validPath(
33
self, n: int, edges: List[List[int]], source: int, destination: int
44
) -> bool:
5-
deffind(x):
6-
ifp[x] !=x:
7-
p[x] =find(p[x])
8-
returnp[x]
5+
g= [[] for_inrange(n)]
6+
fora, binedges:
7+
g[a].append(b)
8+
g[b].append(a)
99

10-
p = list(range(n))
11-
for u, v in edges:
12-
p[find(u)] = find(v)
13-
return find(source) == find(destination)
10+
q = deque([source])
11+
vis = {source}
12+
while q:
13+
i = q.popleft()
14+
if i == destination:
15+
return True
16+
for j in g[i]:
17+
if j not in vis:
18+
vis.add(j)
19+
q.append(j)
20+
return False

0 commit comments

Comments
(0)

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