题目

https://leetcode.com/problems/reconstruct-itinerary/

题解

要把 next 数组按照字典序排列,所以用了 sorted 集合。两个坑:

  • 必须从 JFK 开始
  • 同一个路线会重复出现

最朴素的思路 DFS,还好没超时。分析过程见下图~

后来根据测试用例发现路线会重复。。

import java.util.*;
import java.util.concurrent.ConcurrentSkipListMap;class Vertex {String val;SortedMap<String, Integer> next;public Vertex(String val) {this.val = val;this.next = new ConcurrentSkipListMap<>();}public void incr(String next) {Integer count = this.next.get(next);if (count == null) this.next.put(next, 1);else this.next.put(next, count + 1);}public void decr(String next) {int count = this.next.get(next) - 1;if (count == 0) this.next.remove(next);else this.next.put(next, count);}
}class Solution {int size;public List<String> findItinerary(List<List<String>> tickets) {size = tickets.size();Map<String, Vertex> graph = new HashMap<>();for (List<String> ticket : tickets) {graph.putIfAbsent(ticket.get(0), new Vertex(ticket.get(0)));graph.putIfAbsent(ticket.get(1), new Vertex(ticket.get(1)));graph.get(ticket.get(0)).incr(ticket.get(1));}// dfs 并关注出度Stack<String> stack = new Stack<>();tryDFS(graph, "JFK", stack);System.out.println(stack);return stack;}public boolean tryDFS(Map<String, Vertex> graph, String from, Stack<String> stack) {stack.push(from);System.out.println(graph.get(from).next);if (graph.get(from).next.isEmpty()) {if (stack.size() == size + 1) {return true;} else {stack.pop();return false;}}for (String next : graph.get(from).next.keySet()) { // 只要有一条可行路径 就返回graph.get(from).decr(next);if (tryDFS(graph, next, stack)) return true; // 此路可行else graph.get(from).incr(next); // 此路不可行}// 没有遇到可行路径stack.pop();return false;}
}

leetcode 332. Reconstruct Itinerary | 332. 重新安排行程(Java)相关推荐

  1. 【leetcode】332. Reconstruct Itinerary

    题目如下: Given a list of airline tickets represented by pairs of departure and arrival airports [from, ...

  2. leecode 解题总结:332. Reconstruct Itinerary

    #include <iostream> #include <stdio.h> #include <vector> #include <string> # ...

  3. 「leetcode」332.重新安排行程【回溯算法/深搜】详细图解!

    本文 https://github.com/youngyangyang04/leetcode-master 已经收录,里面还有leetcode刷题攻略.各个类型经典题目刷题顺序.思维导图,可以fork ...

  4. 【LeetCode】332. 重新安排行程

    [LeetCode]332. 重新安排行程 前言 最近在刷回溯专题的题目,前几种题型(排列.组合.子集.子序列)如果直接套用模板,加一些剪枝,随便过的那种 但是这一题如果带入普通的回溯模板,可能无从下 ...

  5. LeetCode刷题流程:回溯算法-13.332. 重新安排行程

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

  6. 代码随想录刷题|LeetCode 332.重新安排行程 51. N皇后 37. 解数独

    目录 332.重新安排行程 思路 重新安排行程 51. N皇后 思路 N皇后 37. 解数独 思路 解数独         这三道题目都是困难题目,都是根据代码随想录的思路总结书写,慢慢理解,慢慢熟练 ...

  7. 代码随想录30——回溯:332重新安排行程、51N皇后、37解数独

    文章目录 1.332重新安排行程 1.1.题目 1.2.解答 1.2.1.思路 1.2.2.代码 2.51N皇后 2.1.题目 2.2.解答 3.37解数独 3.1.题目 3.2.解答 3.2.1.正 ...

  8. 代码随想录算法训练营第30天 | 51. N皇后 37.解数独 332.重新安排行程 回溯篇小结

    代码随想录系列文章目录 回溯篇 - 棋盘问题 图的dfs 文章目录 代码随想录系列文章目录 51.N皇后 37.解数独 332.重新安排行程 回溯篇小结 51.N皇后 题目链接 这道题的思路是什么样的 ...

  9. 代码随想录算法训练营第30天| 332.重新安排行程 、51. N皇后 、 37. 解数独

    代码随想录算法训练营第30天| 332.重新安排行程 .51. N皇后 . 37. 解数独 332.重新安排行程 开始想的是将行程进行全排列之后,然后选出一个字典排序最小的.就也是使用的回溯的思路. ...

最新文章

  1. android studio2.3.2增加jni
  2. python【力扣LeetCode算法题库】面试题13- 机器人的运动范围(BFS)
  3. Spring Cloud Alibaba - 14 OpenFeign自定义配置 + 调用优化 + 超时时间
  4. 神策数据荣登毕马威中国领先消费科技 50 企业
  5. Angular应用里child Component如何向parent Component发送事件
  6. Java HashMap的实现原理详解
  7. idea 错误: 找不到或无法加载主类(汇总贴)
  8. 卧龙吟游戏服务器不显示,卧龙吟你必须知道的隐藏特性
  9. mysql 操作表的例子,mysql中库和表的简单操作总结(附示例)
  10. xpath-了解xpath语言-下载xpath_helper工具
  11. 【django之admin,单例模式】
  12. OSPF定义的5种区域类型:标准区域、主干区域、存根区域、完全存根区域
  13. staitc 变量以及成员
  14. springboot 多模块项目添加一新模块
  15. php之图片上传到服务器的原理
  16. 新书介绍:CCNA基础教程
  17. Excel 技巧大全之 01 如何将公式应用于 Excel 中的整列(5 种简单方法)
  18. 公众号写作排版指南教程v1.0
  19. Accuracy(精度)、Precision(精准度)和Recall(召回率)
  20. JAVA 使用DateUtils对日期进行格式转换

热门文章

  1. c语言的运算符表格,C语言教案(运算符和表格达式).ppt
  2. 视觉注意力机制(上)
  3. 逆向工程核心原理学习笔记(十三):分析abex' crackme #1 的延伸:将参数压入栈
  4. (四)boost库之正则表达式regex
  5. C++线程池原理及创建(转)
  6. C++ STL : 模拟实现STL中的string类
  7. 【爬虫】爬取百度贴吧数据
  8. Linux Kernel TCP/IP Stack|Linux网络硬核系列
  9. 顶级极客技术挑战赛,你敢来挑战吗?| 大神登峰造极
  10. Git/SQL/正则表达式练习平台