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)的更多相关文章

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

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

  2. 【LeetCode】332. Reconstruct Itinerary

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

  3. [leetcode]332. Reconstruct Itinerary

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

  4. 332 Reconstruct Itinerary 重建行程单

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

  5. 332. Reconstruct Itinerary

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

  6. 【LeetCode】Reconstruct Itinerary(332)

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

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

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

  8. LeetCode Reconstruct Itinerary

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

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

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

随机推荐

  1. Go语言学习包(1)之bufio包

    参考网址: https://blog.csdn.net/wangshubo1989/article/details/70177928

  2. Impala与Hive的优缺点和异同

    定位: HIVE:长时间的批处理查询分析 impala:实时交互式SQL查询 impala优缺点优点: 1. 生成执行计划树,不用多次启动job造成多余开销,并且减少中间结果数据写入磁盘,执行速度快 ...

  3. spring boot mysql 事务

    mysql默认 事务自动提交.即:每条insert/update/delete语句,不需要程序手工提交事务,而是mysql自行提交了. 如果我们想实现程序事务提交,需要事先关闭mysql的自动提交事务 ...

  4. java泛型&bean copy list

    参考:https://www.oracle.com/technetwork/cn/articles/java/juneau-generics-2255374-zhs.html E:元素K:键N:数字T ...

  5. Java字符串拆分和字符串连接

    Java字符串拆分/连接 public class LierString{ //------------------------------------------------------------ ...

  6. ubuntu14.04&matlab2015b 测试caffe的Matlab接口

    Step1: 修改caffe-master中的Makefile.config 提示:可以到文件中直接“ctrl+f”,键入相应大写字母即可查找到相应位置. Step2:编译接口.如果之前编译caffe ...

  7. JWT(JSON Web Token)原理简介

    原文:http://www.fengchang.cc/post/114 参考了一下这篇文章:https://medium.com/vandium-software/5-easy-steps-to-un ...

  8. 转 ORACLE约束总结

    https://www.cnblogs.com/kerrycode/archive/2012/05/13/2454614.html 你对ORACLE约束的了解如何?比较模糊还是相当透彻?如果你对下面几 ...

  9. 操作集合的线程安全考虑——java

    运行场景:多个线程同时调用ArrayList存放元素 两个线程A和B,在A线程调用的时候,list中暂时还未有元素存在,此时,list的size值为0,同时A在添加元素的时候,add进了一个元素,此时 ...

  10. django建表报错

     今天在用pycharm创建数据库的表的时候碰见了一个报错,这还是头一次碰见这种奇怪的错误.这里记住希望能避免错误. 我所创建的表是用的django原有的用户表,做了一下继承. class UserI ...