# 重新安排行程

给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。

这个问题和 七桥问题 相同,看能不能一次性的走完机场。

public List<String> findItinerary(List<List<String>> tickets) {
        List<String> res = new ArrayList<>();
        Map<String, PriorityQueue<String>> map = new HashMap<>(16);
//        邻接表
        for (List<String> list :
                tickets) {
            String dep = list.get(0), destination = list.get(1);
            if (!map.containsKey(dep)) {
                map.put(dep, new PriorityQueue<>());
            }
            map.get(dep).offer(destination);
        }
        dfs(map, res, "JFK");
    // 逆序反转
        Collections.reverse(res);
        return res;
    }

    private void dfs(Map<String, PriorityQueue<String>> map, List<String> res, String dep) {
        while (map.containsKey(dep) && map.get(dep).size() > 0) {
            String tem = map.get(dep).poll();
            dfs(map, res, tem);
        }
        res.add(dep);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25