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 ...
随机推荐
- ubuntu常用系统命令
安装升级 查看软件xxx安装内容 dpkg -L xxx 查找软件库中的软件 apt-cache search 正则表达式 或 aptitude search 软件包 显示系统安装包的统计信息 apt ...
- python中bytes类型转换为str类型
使用的原因:基于URL解析报文的时候,要使用str类型,但是提供的确实bytes类型,报错: TypeError: must be str, not bytes 所以就把bytes类型转换为str类型 ...
- selenium处理页面select元素
selenium为网页中选择框元素的获取特别引入了一个Select对象, 引入对象的方式: from selenium.webdriver.support.ui import Select 查询文档可 ...
- 转 oracle 默认自动统计信息 时间修改
############sql3: https://blog.csdn.net/dataminer_2007/article/details/41363417http://blog.51cto.com ...
- SpringMVC什么时候配置 视图解析器
当Action返回的是一个真实路径的时候,视图解析器可不进行配置 当Action返回的是逻辑路径的时候,我们必须要在配置文件中注册视图解析器并为该逻辑路径添加前缀和后缀
- kafka原生producer API
转自https://blog.csdn.net/tianlan996/article/details/80495208 1. 类 public class KafkaProducer<K,V&g ...
- 所有节点配置NTP服务
主节点: 打开vim /etc/ntp.conf文件 For more information about this file, see the man pages # ntp.conf(), ntp ...
- Django重新整理4---ModelForm-set(批量处理数据)
1. #引用modelformset from django.forms.models import modelformset_factory #必须继承forms.ModelForm! class ...
- ubuntu14.04通过 gvm 安装 go语言开发环境
最近用回了ubuntu ,所以打算安装golang学习当下比较火热的这个语言 本来打算使用 sudo apt-get install golang的 安装后发现 是1.2.1不是最新版 所以上网上搜了 ...
- Java学习笔记--继承和多态(下)
1.通过继承来开发超类(superclass) 2.使用super 关键词唤起超类的构造方法 3.在超类中覆盖方法 4.区分override和overload 5.在Object类中探索toStrin ...