链接:LeetCode332
过程:略
思路:看题解,欧拉回路或通路用Hierholzer算法
代码:
class Solution {
Map<String,PriorityQueue<String>> map=new HashMap<>();
LinkedList<String> ans=new LinkedList<>();
public List<String> findItinerary(List<List<String>> tickets) {
for(List<String> ticket:tickets){
String src=ticket.get(0);
String dst=ticket.get(1);
if(!map.containsKey(src)){
map.put(src,new PriorityQueue<String>());
}
map.get(src).offer(dst);
}
dfs("JFK");
Collections.reverse(ans);
return ans;
}
public void dfs(String str){
PriorityQueue<String> hh=map.get(str);
while(hh!=null&&hh.size()>0){
String next=hh.poll();
dfs(next);
}
ans.add(str);
}
}