[Leetcode][第332题][JAVA][重新安排行程][欧拉回路 / 欧拉通路][优先队列][DFS]

239 篇文章 1 订阅
【问题描述】[中等]

在这里插入图片描述

【解答思路】
递归

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

复杂度
在这里插入图片描述

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);
    }
}




【总结】
1. 欧拉回路 / 欧拉通路

在这里插入图片描述

2.优先队列

在这里插入图片描述

3.最大堆/最小堆初始化

最小堆
PriorityQueue minheap = new PriorityQueue();
最大堆
PriorityQueue maxheap = new PriorityQueue((x, y) -> y - x);
PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue maxHeap = new PriorityQueue(11,new Comparator(){ //大顶堆,容量11
@Override
public int compare(Integer i1,Integer i2){
return i2-i1;
}
});

转载链接:https://leetcode-cn.com/problems/reconstruct-itinerary/solution/zhong-xin-an-pai-xing-cheng-by-leetcode-solution/
转载链接:https://leetcode-cn.com/problems/reconstruct-itinerary/solution/javadfsjie-fa-by-pwrliang/
参考链接:https://www.cnblogs.com/yongh/p/9945539.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值