Reconstruct Itinerary
来源:互联网 发布:linux wget 404 编辑:程序博客网 时间:2024/05/24 05:15
一道好题,要常做常新。当中犯了两个错误:
1. 对于[["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]],会有这样的错误: ["JFK","KUL","NRT","JFK"]
2. 对于sjc,是没有queue的,所以必须判断queue为空的情况
public class Solution { List<String> res = new LinkedList<>(); Map<String, PriorityQueue<String>> map = new HashMap<>(); public List<String> findItinerary(String[][] tickets) { if (tickets == null || tickets.length == 0 || tickets[0].length == 0) { return res; } for (String[] ticket: tickets) { if (!map.containsKey(ticket[0])) { PriorityQueue<String> queue = new PriorityQueue<>(); map.put(ticket[0], queue); } map.get(ticket[0]).offer(ticket[1]); } dfs("JFK"); return res; } private void dfs(String str) { //1 res.add(str); PriorityQueue<String> queue = map.get(str); //2 while (!queue.isEmpty()) { while (queue != null && !queue.isEmpty()) { dfs(queue.poll()); } res.add(0, str); }}
0 0
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- Reconstruct Itinerary
- leetcode Reconstruct Itinerary
- *[LeetCode]332. Reconstruct Itinerary
- 332. Reconstruct Itinerary
- leetcode 332. Reconstruct Itinerary
- LeetCode332. Reconstruct Itinerary
- [LeetCode 332] Reconstruct Itinerary
- 332. Reconstruct Itinerary
- LeetCode 332 : Reconstruct Itinerary
- LeetCode:Binary Tree Traversal(二叉树遍历非递归)
- 字典
- Android项目中sdk 23无法使用HttpGet 和HttpClient的问题解决--只需改一行
- web开发之数据安全
- LeetCode 根据前序和中序遍历构造二叉树的三种解法
- Reconstruct Itinerary
- 关于新浪微博里面长图加载功能的一点窥探
- Kubernetes1.3新特性:集群联盟-在全球快速部署和管理应用
- 日志系列
- umeng分享到微信和微信朋友圈只显示ShareContent的bug
- easyUI获取Json的内嵌数据
- pandas入门-数据结构(2)
- Java并发编程(六)阻塞队列
- springmvc乱码