日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

815. Bus Routes

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 815. Bus Routes 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

輸入:int[][] routes routes[i]表示第i號公交車的運行線路。如果routes[i]={1,3,5}。說明公交車運行線路是1->3->5。
   int S:表示起始站點
   int T:表示目的站點
輸出:從S到T最少需要幾輛公交車。
規則:一開始人沒有坐在公交車上,出行方式只用公交車。
分析:很自然的想到以各個站點為節點,站點之間從上一站到下一站作為線。每個站點添加個list維護這個站點屬于哪些公交車。
 這樣的想法很自然,很慣性,確沒有什么用。因為要找的是公交車數量的最小值,所以應該重點關注如何從一個公交車跳轉到另外一個公交車。直到調到包含T的公交車上。
學習1:官方的solution
 把公交車看做是圖的節點。要返回最少的公交車數量,就是一個最短路徑問題。
 如果兩個公交車至少有一個站是相同的,那這兩個公交車之間有連線。
 從起始站開始BFS遍歷圖,直到遇到目標站。
 一個站可能在多個公交車里面有,所以起始隊列和終點目標都是多個的。
 這里發現BFS總能解決最短路徑問題。;例如847,也是最短路徑,也用了BFS。

public int numBusesToDestination(int[][] routes, int S, int T) {//構建圖Map<Integer, List<Integer>> graph = new HashMap<Integer,List<Integer>>();int N = routes.length;for(int i=0;i<N;i++){Arrays.sort(routes[i]);graph.put(i,new ArrayList<Integer>());}for(int i=0;i<N;i++){for(int j = i+1;j<N;j++){if(intersection(routes[i],routes[j])){graph.get(i).add(j);graph.get(j).add(i);}}}//遍歷Queue<Point> queue = new ArrayDeque<>();Set<Integer> seen = new HashSet<>();List<Integer> targets = new ArrayList<>();for(int i=0;i<N;i++){if(Arrays.binarySearch(routes[i],S)!=-1){queue.offer(new Point(i,0));seen.add(i);}if(Arrays.binarySearch(routes[i],T)!=-1){targets.add(i);}}while(!queue.isEmpty()){Point point = queue.poll();int node = point.x;int depth = point.y;if(targets.contains(node)){return depth+1;}else{for(Integer next : graph.get(node)){if(!seen.contains(next)){seen.add(next);queue.offer(new Point(next,depth+1));}}}}return -1;}/*** 判斷兩個數組是否有交集* @param a* @param b* @return*/private boolean intersection(int[] a,int[] b) {int i = 0;int j = 0;while(i<a.length && j<b.length){if(a[i]==b[j]) return true;if(a[i]<b[j]){i++;}else{j++;}}return false;}

學習2:如果把公交站點看做圖中的節點也是可以的。但是不是以公交車的線路作為連線的。而是一次訪問了一輛公交車上所有的站點。理解起來不如上面的解法。

/*** https://leetcode.com/problems/bus-routes/discuss/122712/Simple-Java-Solution-using-BFS* 我一定認為要按照公交車的行駛順利遍歷站點。* 如果roets[0]={1,5,7},如果從5開始,那么1,7可以同時加入隊列,因為只要在這輛公交車內不管走多少遍,都是1輛公交車。題目需求求解的也是最少公交車數量,不是站點數量。* @param routes* @param S* @param T* @return*/public int numBusesToDestination(int[][] routes, int S, int T) {if(S==T) return 0;Map<Integer,List<Integer>> map = new HashMap<Integer, List<Integer>>();for(int i=0;i<routes.length;i++){for(int j=0;j<routes[i].length;j++){List<Integer> busList = map.getOrDefault(routes[i][j],new ArrayList<>());busList.add(i);map.put(routes[i][j],busList);}}Queue<Integer> queue = new ArrayDeque<>();queue.offer(S);Set<Integer> seen = new HashSet<>();int level = 0;while(!queue.isEmpty()){int size = queue.size();level++;for(int i=0;i<size;i++){int stop = queue.poll();if(stop == T) return level;for(int bus : map.get(stop)){if(seen.contains(bus)) continue;seen.add(bus);for(int j=0;j<routes[bus].length;j++){queue.offer(routes[bus][j]);}}}}return -1;}

代碼1

代碼2

總結

以上是生活随笔為你收集整理的815. Bus Routes的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。