leetcode 332. Reconstruct Itinerary BFS多叉树的最小字典序
来源:互联网 发布:中信淘宝v卡积分规则 编辑:程序博客网 时间:2024/05/18 00:52
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
Note:
If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary [“JFK”, “LGA”] has a smaller lexical order than [“JFK”, “LGB”].
All airports are represented by three capital letters (IATA code).
You may assume all tickets form at least one valid itinerary.
Example 1:
tickets = [[“MUC”, “LHR”], [“JFK”, “MUC”], [“SFO”, “SJC”], [“LHR”, “SFO”]]
Return [“JFK”, “MUC”, “LHR”, “SFO”, “SJC”].
Example 2:
tickets = [[“JFK”,”SFO”],[“JFK”,”ATL”],[“SFO”,”ATL”],[“ATL”,”JFK”],[“ATL”,”SFO”]]
Return [“JFK”,”ATL”,”JFK”,”SFO”,”ATL”,”SFO”].
Another possible reconstruction is [“JFK”,”SFO”,”ATL”,”JFK”,”ATL”,”SFO”]. But it is larger in lexical order.
这道题要求找到最小的字典序的序列,是一个典型的DFS深度优先遍历的应用,不
过要注意的是这里的使用了优先队列来实现字典序的比较,注意这里是倒序收集节点信息的!!!
代码如下:
import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.PriorityQueue;/* * 一个简单的DFS写的还是有有问题,需要急需刷第二遍leetcode * */public class Solution{ Map<String, PriorityQueue<String>> map= new HashMap<>(); List<String> res = new ArrayList<>(); public List<String> findItinerary(String[][] tickets) { if(tickets==null || tickets.length<=0) return res; for(int i=0;i<tickets.length;i++) { if(map.containsKey(tickets[i][0])==false) { PriorityQueue<String> one=new PriorityQueue<>(); map.put(tickets[i][0], one); } map.get(tickets[i][0]).add(tickets[i][1]); } dfs("JFK"); Collections.reverse(res); return res; } public void dfs(String key) { if(map.containsKey(key)==false) { res.add(key); return ; }else { PriorityQueue<String> one=map.get(key); while(one.isEmpty()==false) dfs(one.poll()); res.add(key); } } public static void main(String[] args) { Solution solution=new Solution(); String[][] tickets={{"JFK","KUL"},{"JFK","NRT"},{"NRT","JFK"}}; solution.findItinerary(tickets); }}
- leetcode 332. Reconstruct Itinerary BFS多叉树的最小字典序
- *[LeetCode]332. Reconstruct Itinerary
- leetcode 332. Reconstruct Itinerary
- LeetCode *** 332. Reconstruct Itinerary
- LeetCode 332. Reconstruct Itinerary
- leetcode 332. Reconstruct Itinerary
- [leetcode] 332. Reconstruct Itinerary
- Leetcode 332. Reconstruct Itinerary
- Leetcode 332. Reconstruct Itinerary
- Leetcode 332. Reconstruct Itinerary
- [LeetCode]332. Reconstruct Itinerary
- Leetcode: 332.Reconstruct Itinerary
- leetcode:332. Reconstruct Itinerary
- LeetCode 332. Reconstruct Itinerary
- [leetcode]332. Reconstruct Itinerary
- [LeetCode] 332. Reconstruct Itinerary
- leetcode 332. Reconstruct Itinerary
- [LeetCode]332. Reconstruct Itinerary
- JS匿名函数理解
- 【Typical】几道题的极简主义题解
- 【Oracle】Oracle创建实例
- Unity3d各种窗口简单介绍
- shell脚本执行oracle数据库脚本.sql
- leetcode 332. Reconstruct Itinerary BFS多叉树的最小字典序
- C/C++面试题(二)
- Linux根目录下各目录文件类型及各项缩写全称
- Spring 多线程与异步编程
- Linux配置防火墙详细步骤(iptables命令使用方法)
- 无法升级数据库 'SchoolPlatForm1',因为它是只读的,或者具有只读文件。请将数据库或文件设为可写,然后重新运行恢复操作。 (Microsoft SQL Server,错误: 3415)
- POJ3264-Balanced Lineup
- java内存机制中的堆栈常量池简介
- C/C++面试题(三)