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 }

 

posted on 2016-07-01 15:31  keepshuatishuati  阅读(148)  评论(0编辑  收藏  举报