332 Reconstruct Itinerary 重建行程单
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:
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"].
All airports are represented by three capital letters (IATA code).
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.
详见:https://leetcode.com/problems/reconstruct-itinerary/description/
C++:
class Solution {
public:
vector<string> findItinerary(vector<pair<string, string> > tickets) {
vector<string> res;
unordered_map<string, multiset<string> > m;
for (auto a : tickets)
{
m[a.first].insert(a.second);
}
dfs(m, "JFK", res);
return vector<string> (res.rbegin(), res.rend());
}
void dfs(unordered_map<string, multiset<string> > &m, string s, vector<string> &res) {
while (m[s].size())
{
string t = *m[s].begin();
m[s].erase(m[s].begin());
dfs(m, t, res);
}
res.push_back(s);
}
};
参考:https://www.cnblogs.com/grandyang/p/5183210.html
332 Reconstruct Itinerary 重建行程单的更多相关文章
- [LeetCode] Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...
- [leetcode]332. Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...
- 332. Reconstruct Itinerary
class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...
- 332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs -- graph <String, List<String>>, (the value is sorted a ...
- Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- LeetCode Reconstruct Itinerary
原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...
随机推荐
- 团队开发git使用各种问题
参考:https://www.cnblogs.com/schaepher/p/4933873.html 问题-3:保持github上项目干净,对于在不同机器上运行会不同的文件不予维护(如.idea/w ...
- HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)
HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...
- Hihocoder 1329(splay)
Problem 平衡树 Splay 题目大意 维护一个数列,支持三种操作. 操作1:添加一个数x. 操作2:询问不超过x的最大的数. 操作三:删除大小在区间[a,b]内的数. 解题分析 和上一题相比, ...
- sharepoint 2013安装--没安装成功--机器配置太低了
油管上的sharepoint2013安装教程 https://www.youtube.com/watch?v=3lQVMGWJQho 下载脚本的网址 http://gallery.technet.mi ...
- HDU——4738 Caocao's Bridges
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ubuntu 图形界面搜索软件Catfish (鲶鱼)
Catfish(鲶鱼)是一款Linux桌面图形软件,可以在桌面图形化的搜索文件. 在Ubuntu下可以用 : sudo apt-get install catfish 安装
- leetcode笔记:Wiggle Sort
一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...
- Date类型转换为Integer类型
Date类型转换为Integer类型: Integer date = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMd ...
- HDU 5289 Assignment (二分+区间最值)
[题目链接]click here~~ [题目大意]: 给出一个数列,问当中存在多少连续子序列,子序列的最大值-最小值<k [思路]:枚举数列左端点.然后二分枚举右端点,用ST算法求区间最值.(或 ...
- LNMP环境搭建——PHP篇
一.源代码安装 1.编译安装 ./configure --prefix=/usr/local/php\ --with-config-file-path=/usr/local/php/etc --wit ...