【问题描述】[中等]

【解答思路】

递归



复杂度

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

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

  1. [Leetcode][第889题][JAVA][根据前序和后序遍历构造二叉树][分治][递归]

    [问题描述][中等] [解答思路] copyOfRange class Solution {public TreeNode constructFromPrePost(int[] pre, int[] ...

  2. [Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]

    [问题描述][中等] [解答思路] public class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) { ...

  3. [Leetcode][第78题][JAVA][子集][位运算][回溯]

    [问题描述][中等] [解答思路] 1. 位运算 复杂度 class Solution {List<Integer> t = new ArrayList<Integer>(); ...

  4. [Leetcode][第79题][JAVA][单词搜索][DFS][回溯]

    [问题描述][中等] [解答思路] 1. DFS繁琐版本 class Solution {public boolean exist(char[][] board, String word) {bool ...

  5. [Leetcode][第40题][JAVA][数组总和2][回溯][剪枝]

    [问题描述][中等] [解答思路] 1. 减法 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Ar ...

  6. [Leetcode][第216题][JAVA][数组之和3][回溯]

    [问题描述][中等] [解答思路] 回溯 剪树枝 当和超过n 或 个数超过k 1. 正向求和 优化前 class Solution {public List<List<Integer> ...

  7. [Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]

    [问题描述][中等] [解答思路] 1. 回溯 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.De ...

  8. [Leetcode][第77题][JAVA][组合][回溯]

    [问题描述][中等] [解答思路] 1. 回溯 class Solution {List<List<Integer>> lists = new ArrayList<> ...

  9. [Leetcode][第17题][JAVA][电话号码的字母组合][回溯]

    [问题描述][中等] [解答思路] 用哈希表/数组存储每个数字对应的所有可能的字母,然后进行回溯操作. 回溯过程中维护一个字符串,表示已有的字母排列(如果未遍历完电话号码的所有数字,则已有的字母排列是 ...

最新文章

  1. 现代密码学1.2--Kerckhoffs原则
  2. DBA用于查询当前数据库表格记录条数的脚本
  3. java exception子类_Java异常 Exception类及其子类(实例讲解)
  4. java 画笔跟swing组件_「软帝学院」:2019思维最清晰的java学习路线
  5. 22 省遭受重大洪灾,机器学习未来能预报么?
  6. SPFieldLookupValue
  7. It's my life
  8. 学习mongo系列(五) AND,$or,$type
  9. axure轮播图怎么设置循环轮播_Axure教程:轮播图制作步骤详解
  10. 视频转码(Java)
  11. 什么软件可以测试睡眠质量心率,Beddit:粘在床上就能测试心率的睡眠监测器
  12. Tecplot 2018 破解失败怎么办?
  13. safenet 超级狗 加密狗
  14. 1153. 字符串排序
  15. 常毅 华为计算机,华为路由器交换机配置.docx
  16. 凌晨3点不回家-现实版
  17. 交大昂立华为鸿蒙,20210517湖南人涨停复盘
  18. Photoshop中的填充功能
  19. 蓝牙助手的介绍以及使用
  20. 在Excel工作表数据输入表单中编辑记录

热门文章

  1. MongoDB的基本shell操作(三)
  2. 【市场调研与分析】Intel发力移动安全领域——By Me at 20140613
  3. linux 新建用户、用户组 以及为新用户分配权限
  4. Action和Func区别
  5. 【100亿次的挑战】之春晚控制后台故事分享
  6. 关于 block的一些浅识
  7. 实例1.2:获得应用程序主窗口指针
  8. php call()函数,PHP中__call()方法详解
  9. 分层和分段用什么符号_小编带你学直播——后牙树脂分层堆塑
  10. centos7 复制移动删除