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. shell入门基础必备

    作者:KornLee 2005-02-03 15:32:57 来自:Linux先生    1.建立和运行shell程序   什么是shell程序呢? 简单的说shell程序就是一个包含若干行 shel ...

  2. eclipse安装阿里规范模板

    https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化

  3. Lonsdor K518ISE Key Programmer Review

    Lonsdor K518ISE key programmer is the latest version of Lonsdor, with wider vehicle coverage in key ...

  4. Vue.js-----轻量高效的MVVM框架(六、Class与Style绑定)

    这个相对来说简单,看一遍代码就懂. 一.完整片段: <!DOCTYPE html> <html> <head> <meta charset="UTF ...

  5. swagger demo code

    //Application 开启注解 @EnableSwagger2public class Application { public static void main(String[] args) ...

  6. Vue 参数传递及刷新后依旧存在

    获取参数方式有两种: 1.params2.query 第一种方式: params this.$router.push({name:'Hello',params:{name:'zs',age:'22'} ...

  7. sqoop导出数据

    export是HDFS里的文件导出到RDBMS的工具,不能从hive.hbase导出数据,且HDFS文件只能是文本格式.如果要把hive表数据导出到RDBMS,可以先把hive表通过查询写入到一个临时 ...

  8. java && C# 线程

      1.多个线程用到同一个资源的话,必须lock 2.为了解决,在竞争的情况下,优先分配资源给A.就是A和B线程都同时在同一时刻需要资源x,然后的话也不清楚系统是具体怎样调度的.或者说怎样调度,都有可 ...

  9. linux 查看页大小

    # getconf PAGE_SIZE 一般是4096

  10. (转)shell脚本输出带颜色字体

    shell脚本输出带颜色字体 原文:http://blog.csdn.net/andylauren/article/details/60873400 输出特效格式控制:\033[0m  关闭所有属性  ...