Reconstruct Itinerary

2024-01-04 12:18
文章标签 reconstruct itinerary

本文主要是介绍Reconstruct Itinerary,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


一道好题,要常做常新。当中犯了两个错误:

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);}
}


这篇关于Reconstruct Itinerary的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/569309

相关文章

LeetCode: Reconstruct Original Digits from English

这道 LeetCode 上的题目,还是有点难度,有点意思的。就是要把给定的字符串中的英文,组合成 0,1,2,3,4,5,6,7,8,9 0,1,2,3,4,5,6,7,8,9 这几个数字的英文,再按升序将数字输出。 具体的,题目描述如下: Given a non-empty string containing an out-of-order English representation

Convenient itinerary technical support

If you have any questions, please contact us in time on the official website