首页 > 其他 > 详细

Reconstruct Itinerary

时间:2016-07-01 15:56:47      阅读:113      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!