class Solution {
public boolean validPath(int n, int[][] edges, int source, int destination) {
List<Integer>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int[] edge : edges) {
int x = edge[0], y = edge[1];
adj[x].add(y);
adj[y].add(x);
}
boolean[] visited = new boolean[n];
Queue<Integer> queue = new ArrayDeque<Integer>();
queue.offer(source);
visited[source] = true;
while (!queue.isEmpty()) {
int vertex = queue.poll();
if (vertex == destination) {
break;
}
for (int next : adj[vertex]) {
if (!visited[next]) {
queue.offer(next);
visited[next] = true;
}
}
}
return visited[destination];
}
}
2022.12.19-----leetcode.1971
最新推荐文章于 2024-09-30 13:43:16 发布