|
35 | 35 | <pre> |
36 | 36 | <strong>输入:</strong>routes = [[1,2,7],[3,6,7]], source = 1, target = 6 |
37 | 37 | <strong>输出:</strong>2 |
38 | | -<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。 |
| 38 | +<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。 |
39 | 39 | </pre> |
40 | 40 |
|
41 | 41 | <p><strong>示例 2:</strong></p> |
@@ -367,55 +367,56 @@ function numBusesToDestination(routes: number[][], source: number, target: numbe |
367 | 367 | public class Solution { |
368 | 368 | public int NumBusesToDestination(int[][] routes, int source, int target) { |
369 | 369 | if (source == target) { |
370 | | - return 0; |
| 370 | + return 0; // 如果起点和终点相同,直接返回 0 |
371 | 371 | } |
372 | 372 |
|
373 | | - Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>(); |
374 | | - List<HashSet<int>> routeToStops = new List<HashSet<int>>(); |
375 | | - |
| 373 | + // 使用 Dictionary 构建站点到公交线路的映射 |
| 374 | + var g = new Dictionary<int, List<int>>(); |
376 | 375 | for (int i = 0; i < routes.Length; i++) { |
377 | | - routeToStops.Add(new HashSet<int>()); |
378 | 376 | foreach (int stop in routes[i]) { |
379 | | - routeToStops[i].Add(stop); |
380 | | - if (!stopToRoutes.ContainsKey(stop)) { |
381 | | - stopToRoutes[stop] = new HashSet<int>(); |
| 377 | + if (!g.ContainsKey(stop)) { |
| 378 | + g[stop] = new List<int>(); |
382 | 379 | } |
383 | | - stopToRoutes[stop].Add(i); |
| 380 | + g[stop].Add(i); // 将公交线路编号添加到该站点的列表中 |
384 | 381 | } |
385 | 382 | } |
386 | 383 |
|
387 | | - Queue<int> queue = new Queue<int>(); |
388 | | - HashSet<int> visited = new HashSet<int>(); |
389 | | - int ans = 0; |
390 | | - |
391 | | - foreach (int routeId in stopToRoutes[source]) { |
392 | | - queue.Enqueue(routeId); |
393 | | - visited.Add(routeId); |
| 384 | + // 如果 source 或 target 不在站点映射中,返回 -1 |
| 385 | + if (!g.ContainsKey(source) || !g.ContainsKey(target)) { |
| 386 | + return -1; |
394 | 387 | } |
395 | 388 |
|
396 | | - while (queue.Count > 0) { |
397 | | - int count = queue.Count; |
398 | | - ans++; |
| 389 | + // 初始化队列和访问集合 |
| 390 | + var q = new Queue<int[]>(); |
| 391 | + var visBus = new HashSet<int>(); // 记录访问过的公交线路 |
| 392 | + var visStop = new HashSet<int>(); // 记录访问过的站点 |
| 393 | + q.Enqueue(new int[] { source, 0 }); // 将起点加入队列,公交次数初始化为 0 |
| 394 | + visStop.Add(source); // 将起点标记为已访问 |
399 | 395 |
|
400 | | - for (int i = 0; i < count; i++) { |
401 | | - int routeId = queue.Dequeue(); |
| 396 | + // 开始广度优先搜索 |
| 397 | + while (q.Count > 0) { |
| 398 | + var current = q.Dequeue(); // 从队列中取出当前站点 |
| 399 | + int stop = current[0], busCount = current[1]; |
402 | 400 |
|
403 | | - foreach (int stop in routeToStops[routeId]) { |
404 | | - if (stop == target) { |
405 | | - return ans; |
406 | | - } |
| 401 | + // 如果当前站点是目标站点,返回所需的公交次数 |
| 402 | + if (stop == target) { |
| 403 | + return busCount; |
| 404 | + } |
407 | 405 |
|
408 | | - foreach (int nextRoute in stopToRoutes[stop]) { |
409 | | - if (!visited.Contains(nextRoute)) { |
410 | | - visited.Add(nextRoute); |
411 | | - queue.Enqueue(nextRoute); |
| 406 | + // 遍历经过当前站点的所有公交线路 |
| 407 | + foreach (int bus in g[stop]) { |
| 408 | + if (visBus.Add(bus)) { // 如果公交线路没有被访问过 |
| 409 | + // 遍历该线路上的所有站点 |
| 410 | + foreach (int nextStop in routes[bus]) { |
| 411 | + if (visStop.Add(nextStop)) { // 如果该站点没有被访问过 |
| 412 | + q.Enqueue(new int[] { nextStop, busCount + 1 }); // 将新站点加入队列,公交次数加 1 |
412 | 413 | } |
413 | 414 | } |
414 | 415 | } |
415 | 416 | } |
416 | 417 | } |
417 | 418 |
|
418 | | - return -1; |
| 419 | + return -1; // 如果无法到达目标站点,返回 -1 |
419 | 420 | } |
420 | 421 | } |
421 | 422 | ``` |
|
0 commit comments