332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs
-- graph <String, List<String>>, (the value is sorted and non- duplicate)
Collections.sort(value)
2. dfs
we use bottom-up to store the path and reverse it finally. And to check if visited or not, we remove the node.
dfs(node){
if(node == null) return;
visit(node);
visitedp[node] = true;
for(each neightbors from node){
if(! visited neighbors) dfs(neighbors);
}
}
Here we use the adjacent list
dfs(graph, curKey){
if(!garph.containsKey(curKey) || graph.get(curKey).size()==0)
return;
//visit (top down)
while(graph.get(curKey).size()){
String temp = graph.get(curKey).get(0);
graph.get(curKey).remove(0); //remvoe the path
dfs(graph, temp);
//visit bottom up
}
}
Solution
class Solution {
//what is the problem of top down
//solve this by bottom up
List<String> res = new ArrayList<>();
public List<String> findItinerary(String[][] tickets) {
//build graph
Map<String, List<String>> graph = new HashMap<>();
for (String[] ticket : tickets) {
if (!graph.containsKey(ticket[0])) graph.put(ticket[0], new ArrayList<>()); //contains check the null first
graph.get(ticket[0]).add(ticket[1]);
}
//sorting the value by value
for (String key : graph.keySet()) {
Collections.sort(graph.get(key));
}
dfs("JFK",graph);
res.add("JFK");
Collections.reverse(res);
return res;
}
void dfs(String cur,Map<String, List<String>> graph){
if(!graph.containsKey(cur) || graph.get(cur).size() == 0) return;
//res.add(graph.get(cur).get(0));
//seach all the list
while(graph.get(cur).size()!=0){
String temp = graph.get(cur).get(0);//
graph.get(cur).remove(0);
dfs(temp,graph);
res.add(temp);
}
}
}
how to build graph efficiently?
332. Reconstruct Itinerary (leetcode)的更多相关文章
- 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...
- 【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 ...
- 332 Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 332. Reconstruct Itinerary
class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- [LeetCode] Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- LeetCode Reconstruct Itinerary
原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...
- [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
随机推荐
- 华东交通大学2015年ACM“双基”程序设计竞赛1002
Problem B Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- 解决IDEA卡顿问题及相关基本配置
https://blog.csdn.net/u013068377/article/details/54316965 https://blog.csdn.net/u014527619/article/d ...
- Ubuntu 安装 phpredis扩展
官网 https://github.com/phpredis/phpredis 下载->然后解压->上传服务器 /etc/phpredis 进行 cd /etc/phpredisphpiz ...
- NIOGoodDemo
Java NIO是在jdk1.4开始使用的,它既可以说成“新I/O”,也可以说成非阻塞式I/O.下面是java NIO的工作原理: 1. 由一个专门的线程来处理所有的 IO 事件,并负责分发. 2. ...
- Java程序员进阶架构师推荐阅读书籍
[IT168 技术]作为Java程序员来说,最痛苦的事情莫过于可以选择的范围太广,可以读的书太多,往往容易无所适从.我想就我自己读过的技术书籍中挑选出来一些,按照学习的先后顺序,推荐给大家,特别是那些 ...
- 2019.03.20 读书笔记 关于Reflect与Emit的datatable转list的效率对比
Reflect public static List<T> ToListByReflect<T>(this DataTable dt) where T : new() { Li ...
- Linux环境常用命令
bash host #查看IP对应机器名 hostname #查看本机名 hostname –i #查看本机IP mssh ...
- Redis Intro - Skiplist
http://zhangtielei.com/posts/blog-redis-skiplist.html https://juejin.im/entry/59197a390ce4630069fbcf ...
- UGUI ScrollRect 各参数的代码引用以及作用
- Unity 双击Esc或者返回退出游戏,有文字提示
第一次点击Esc或者返回,显示提示文字"再次按下返回键退出游戏",在文字消失之前再次点击Esc或者返回,退出游戏. 此脚本挂在Text文字提示上: using UnityEngin ...