【问题描述】[中等]
【解答思路】
递归
复杂度
class Solution {Map<String, PriorityQueue<String>> map = new HashMap<String, PriorityQueue<String>>();List<String> itinerary = new LinkedList<String>();public List<String> findItinerary(List<List<String>> tickets) {for (List<String> ticket : tickets) {String src = ticket.get(0), dst = ticket.get(1);if (!map.containsKey(src)) {map.put(src, new PriorityQueue<String>());}map.get(src).offer(dst);}dfs("JFK");Collections.reverse(itinerary);return itinerary;}public void dfs(String curr) {while (map.containsKey(curr) && map.get(curr).size() > 0) {String tmp = map.get(curr).poll();dfs(tmp);}itinerary.add(curr);}
}
【总结】
1. 欧拉回路 / 欧拉通路
2.优先队列
3.最大堆/最小堆初始化
最小堆
PriorityQueue minheap = new PriorityQueue();
最大堆
PriorityQueue maxheap = new PriorityQueue((x, y) -> y - x);
PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue maxHeap = new PriorityQueue(11,new Comparator(){ //大顶堆,容量11
@Override
public int compare(Integer i1,Integer i2){
return i2-i1;
}
});
转载链接:https://leetcode-cn.com/problems/reconstruct-itinerary/solution/zhong-xin-an-pai-xing-cheng-by-leetcode-solution/
转载链接:https://leetcode-cn.com/problems/reconstruct-itinerary/solution/javadfsjie-fa-by-pwrliang/
参考链接:https://www.cnblogs.com/yongh/p/9945539.html