码迷,mamicode.com
首页 > 其他好文 > 详细

Reconstruct Itinerary

时间:2016-07-01 15:59:23      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

 1 public class Solution {
 2     public List<String> findItinerary(String[][] tickets) {
 3         List<String> result = new ArrayList<>();
 4         if (tickets.length == 0 || tickets[0].length == 0) {
 5             return result;
 6         }
 7         Map<String, PriorityQueue<String>> iternary = new HashMap<>();
 8         for (String[] fromTo : tickets) {
 9             if (!iternary.containsKey(fromTo[0])) {
10                 iternary.put(fromTo[0], new PriorityQueue<>());
11             }
12             iternary.get(fromTo[0]).add(fromTo[1]);
13         }
14         
15         Stack<String> stack = new Stack<>();
16         stack.push("JFK");
17         
18         while (!stack.isEmpty()) {
19             String current = stack.peek();
20             if (iternary.containsKey(current) && iternary.get(current).size() > 0) {
21                 stack.push(iternary.get(current).poll());
22             } else {
23                 result.add(0, stack.pop());
24             }
25         }
26         return result;
27     }
28 }

 

Reconstruct Itinerary

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/5633242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!