赞
踩
题目:
题解:
- class Solution {
- Map<String, PriorityQueue<String>> map = new HashMap<String, PriorityQueue<String>>();
- List<String> itinerary = new LinkedList<String>();
-
- public List<String> findItinerary(List<List<String>> tickets) {
- for (List<String> ticket : tickets) {
- String src = ticket.get(0), dst = ticket.get(1);
- if (!map.containsKey(src)) {
- map.put(src, new PriorityQueue<String>());
- }
- map.get(src).offer(dst);
- }
- dfs("JFK");
- Collections.reverse(itinerary);
- return itinerary;
- }
-
- public void dfs(String curr) {
- while (map.containsKey(curr) && map.get(curr).size() > 0) {
- String tmp = map.get(curr).poll();
- dfs(tmp);
- }
- itinerary.add(curr);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。