leetcode-332-Reconstruct Itinerary
来源:互联网 发布:js设置全局cookie 编辑:程序博客网 时间:2024/05/20 06:53
这道题考的是DFS、有向图、欧拉路径(一笔画问题)、Hierholzer’s 算法。
思路:把问题抽象成一个图,求一笔画路径,还要保持最小序输出,所以应该用multiset存储一个起点的目的地。想到map
class Solution {public: void dfs(string airport) { while (map1[airport].size()) { string temp = *map1[airport].begin(); map1[airport].erase(map1[airport].begin()); dfs(temp); } ret.push_back(airport); } map<string, multiset<string>> map1; vector<string> ret; vector<string> findItinerary(vector<pair<string, string>> tickets) { for (auto i : tickets) { map1[i.first].insert(i.second); } dfs("JFK"); return vector<string>(ret.rbegin(), ret.rend()); }};
另外,priority_queue也是可以的:
C++代码:
class Solution {public: void dfs(string airport) { while (map1[airport].size()) { string temp = (map1[airport]).top(); map1[airport].pop(); dfs(temp); } ret.push_back(airport); } map<string, priority_queue<string,vector<string>,greater<string>>> map1; vector<string> ret; vector<string> findItinerary(vector<pair<string, string>> tickets) { for (auto i : tickets) { map1[i.first].push(i.second); } dfs("JFK"); return vector<string>(ret.rbegin(), ret.rend()); }};
0 0
- [LeetCode 332] Reconstruct Itinerary
- LeetCode 332 : Reconstruct Itinerary
- leetcode 332 Reconstruct Itinerary
- LeetCode[332] Reconstruct Itinerary
- leetcode-332-Reconstruct Itinerary
- Leetcode 332 Reconstruct Itinerary
- [leetcode-332]Reconstruct Itinerary(java)
- leetcode Reconstruct Itinerary
- *[LeetCode]332. Reconstruct Itinerary
- leetcode 332. Reconstruct Itinerary
- leetcode笔记:Reconstruct Itinerary
- LeetCode *** 332. Reconstruct Itinerary
- leetcode reconstruct-itinerary
- LeetCode 332. Reconstruct Itinerary
- 【Leetcode】Reconstruct Itinerary
- LeetCode No332. Reconstruct Itinerary
- leetcode 332. Reconstruct Itinerary
- [leetcode] 332. Reconstruct Itinerary
- Linux下使用Qt Creator开发OpenCV项目-2.测试开发环境
- tomcat 入门
- 【IMWeb训练营作业】用Vue做简单的自定义select
- C#学习日记2017-04-22 NPOI用法小结(未完待续)
- React-Native中一些组件的用法(一)
- leetcode-332-Reconstruct Itinerary
- ajax关于对象传后台问题
- S5PV210 uboot中HOSTARCH和HOSTOS两个环境变量详解
- 【错误积累】Ambiguous mapping found. Cannot map '***Controller' bean method
- Redis 内部数据结构详解(1):dict
- js冒泡事件的作用效果
- GDOI冲刺训练模拟赛第三套总结
- DZ3.3邮箱发送不成功解决方法
- 百万级访问网站前期的技术准备