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. HDU计算机学院大学生程序设计竞赛(2015’12)The Country List

    Problem Description As the 2010 World Expo hosted by Shanghai is coming, CC is very honorable to be ...

  2. 三个键print scroll、pause

    上班族或是办公室白领每天都几乎跟键盘打交道, 那么键盘上的PrtSc SysRq(print screen).Scroll Lock.se Break(pause break).numlock等有何作 ...

  3. Go语言基础之9--指针类型详解

    一. 变量和内存地址 每个变量都有内存地址,可以说通过变量来操作对应大小的内存 注意:通过&符号可以获取变量的内存地址 通过下面例子来理解下: 实例1-1 package main impor ...

  4. Python模块:operator简单介绍

    Python官方文档地址:https://docs.python.org/3.6/library/operator.html?highlight=operator Operator提供的函可用于对象比 ...

  5. pip 安装库的时候使用豆瓣镜像 提升效率

    由于众所周知的原因,国内网络环境始终处于水深火热之中,python库的安装也不例外. 比如在安装 PyQt5-tools 的时候,网速奇慢无比. 好在国内有不少镜像服务源,以豆瓣为例,网速突飞猛进 使 ...

  6. Should I buy Auro OtoSys IM600 or Obdstar X300 DP?

    Auro OtoSys IM600 and Obdstar X300 DP – What’s the difference & Which better? This is for those ...

  7. POI 按word模版生成合同并生成PDF

    功能需求:根据用户给的word版本合同文件.docx,实现模版替换功能. 如: 功能:支持段落和表格里的文字替换,还可以支持表格替换.如果需要段落需要换行用<br>隔开如:身份证<b ...

  8. 使用PIE对IE6、7、8进行CSS3兼容介绍和经验总结

    下面说说如何对 IE10 以下版本的浏览器进行部分 CSS3 兼容 国外团队开发的兼容插件,去年做项目时才发现,非常强大 主角:PIE.js ,  PIE.htc    两种方法可以实现 官方网站:h ...

  9. [引]雅虎日历控件 Example: Two-Pane Calendar with Custom Rendering and Multiple Selection

    本文转自:http://yuilibrary.com/yui/docs/calendar/calendar-multipane.html This example demonstrates how t ...

  10. (转)sdd for aix 安装及基本命令

    总结出自多个文件(自己做的项目和网上找的资料) 原文:http://blog.csdn.net/yujin2010good/article/details/11395701 一.sddpcm安装 要安 ...