Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

Note:

  1. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
  2. All airports are represented by three capital letters (IATA code).
  3. You may assume all tickets form at least one valid itinerary.

Example 1:
tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"].

Example 2:
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.

重建行程单,在图中找一条路径,能经过所有的边。

参考:http://www.cnblogs.com/grandyang/p/5183210.html

  1. class Solution{
  2. public:
  3. vector<string> findItinerary(vector<pair<string,string>> tickets){\
  4. unordered_map<string,multiset<string>> m;
  5. for(auto t : tickets){
  6. m[t.first].insert(t.second);
  7. }
  8. vector<string> res;
  9. dfs(m,"JFK",res);
  10. return vector<string> (res.rbegin(),res.rend());
  11. }
  12.  
  13. void dfs(unordered_map<string,multiset<string>>& m,string s,vector<string>& res){
  14. while(m[s].size()){
  15. string t = *m[s].begin();
  16. m[s].erase(m[s].begin());
  17. dfs(m,t,res);
  18. }
  19. res.push_back(s);
  20. }
  21. };

Reconstruct Itinerary的更多相关文章

  1. [LeetCode] Reconstruct Itinerary 重建行程单

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  2. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  3. LeetCode Reconstruct Itinerary

    原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...

  4. 【LeetCode】332. Reconstruct Itinerary

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

  5. [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  6. [leetcode]332. Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  7. 332 Reconstruct Itinerary 重建行程单

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  8. 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...

  9. 332. Reconstruct Itinerary

    class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...

随机推荐

  1. c#文本框限制输入内容

         //限制输入不能为中文和全角         private void zhbh_KeyPress(object sender, KeyPressEventArgs e)         { ...

  2. (满满的是硬货)Spring深入研究一IOC实现

    IOC基于Java底层的反射机制实现 反射机制: 核心: Class cls = Class.forName(类名); Class ptypes[] = new Class[2]; ptypes[0] ...

  3. 将 Tor socks 转换成 http 代理

    你可以通过不同的 Tor 工具来使用 Tor 服务,如 Tor 浏览器.Foxyproxy 和其它东西,像 wget 和 aria2 这样的下载管理器不能直接使用 Tor socks 开始匿名下载,因 ...

  4. docker index服务概述

    index顾名思义“索引”,index服务主要提供镜像索引以及用户认证的功能.当下载一个镜像的时候,首先会去index服务上 做认证,然后查找镜像所在的registry的地址并放回给docker客户端 ...

  5. javascript运算符与表达式

    表达式 表达式是关键字.运算符.变量以及文字的组合,用来生成字符串.数字或对象.一个表达式可以完成计算.处理字符.调用函数.或者验证数据等操作. 表达式的值是表达式运算的结果,常量表达式的值就是常量本 ...

  6. 高性能javascript(记录一)

    脚本位置:将js脚本放置在body底部,由于脚本会阻塞页面渲染,导致明显延迟,通常表现为空白页面,用户无法游览页面的内容,也无法与页面进行交互.故因此推荐js脚本放在body底部,尽可能减少对整个页面 ...

  7. ROS语音交互——科大讯飞语音合成TTS(二)

    之前我用过科大讯飞的语音包,为了记录一下我重新使用一下 首先注册科大讯飞账号及应用,以后每个下载的在线使用SDK都是以此账户ID登录讯飞语音服务器. 下载科大讯飞在线合成包. $ unzip Linu ...

  8. javadoc 生成自定义的标签

    转自:http://www.blogjava.net/lishunli/archive/2010/01/12/309218.html Technorati 标记: tools 关键词 个性化地生成Ja ...

  9. JavaScript 跨域小总结

    一. 什么是跨域 域名分为:一级域名.二级域名.三级域名.例如:baidu.com(一级域名) .www.baidu.com(二级域名)tieba.baidu.com(二级域名).bbs.youa.b ...

  10. ruby 编译安装,gem国内源ruby.taobao.org

    centos6.6final 一.安装依赖包(使用默认CENTOS更新源): # yum install openssl* openssl-devel zlib-devel gcc gcc-c++ m ...