欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【leetcode】332. Reconstruct Itinerary

程序员文章站 2022-03-07 17:25:49
...

欧拉通路

提交代码

class Solution {
	Map<String, PriorityQueue<String>> graph =new HashMap<>();
	List<String> path=new LinkedList<>();
	
    public List<String> findItinerary(List<List<String>> tickets) {
       for(List<String> t: tickets)
//    	   graph.getOrDefault(t.get(0), new PriorityQueue<String>()).offer(t.get(1));
    	   graph.computeIfAbsent(t.get(0), k->new PriorityQueue<String>()).add(t.get(1));
       visit("JFK");
       return path;
    }
    
    private void visit(String airport) {
    	while(graph.containsKey(airport)&&graph.get(airport).size()>0)
    		visit(graph.get(airport).poll());
    	path.add(0, airport);
    }
}

运行结果

【leetcode】332. Reconstruct Itinerary

相关标签: leetcode