题目地址:

https://www.lintcode.com/problem/path-search/description

给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径,简单路径是指每个顶点最多只经过一次。要求路径按照字典序排序。

只需先建图,接着DFS的方法遍历一遍即可。注意,因为要按照字典序排序,所以建图的时候,邻居点要用TreeSet来存,这样在遍历路径的时候,字典序小的路径会先遍历。代码如下:

import java.util.*;public class Solution {/*** @param n: The number of points* @param G: The description of graph* @param S: The point S* @param T: The point T* @return: output all the paths from S to T*/public List<List<Integer>> getPath(int n, int[][] G, int S, int T) {// Write your code hereMap<Integer, Set<Integer>> graph = buildGraph(G);List<List<Integer>> res = new ArrayList<>();// cur表示当前路径,visited表示已经访问过的顶点。首先要把源点S加进去List<Integer> cur = new ArrayList<>();cur.add(S);Set<Integer> visited = new HashSet<>();visited.add(S);dfs(S, T, cur, res, visited, graph);return res;}private void dfs(int S, int T, List<Integer> cur, List<List<Integer>> res, Set<Integer> visited, Map<Integer, Set<Integer>> graph) {if (S == T) {res.add(new ArrayList<>(cur));return;}// 要验证一下在不在graph里,否则如果S没有邻居的话,就会造成NPEif (graph.containsKey(S)) {for (int neighbor : graph.get(S)) {if (!visited.contains(neighbor)) {cur.add(neighbor);visited.add(neighbor);dfs(neighbor, T, cur, res, visited, graph);// 回到上一层递归的时候要回溯,恢复现场visited.remove(neighbor);cur.remove(cur.size() - 1);}}}}private Map<Integer, Set<Integer>> buildGraph(int[][] G) {Map<Integer, Set<Integer>> graph = new HashMap<>();for (int[] edge : G) {// 邻居节点要用TreeSet存,这样遍历邻居的时候会从小到大遍历,路径也会按照字典序从小到大遍历graph.putIfAbsent(edge[0], new TreeSet<>());graph.putIfAbsent(edge[1], new TreeSet<>());graph.get(edge[0]).add(edge[1]);graph.get(edge[1]).add(edge[0]);}return graph;}
}

时空复杂度O(V+E)O(V+E)O(V+E)。

【Lintcode】1647. Path Search相关推荐

  1. 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题

    [LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...

  2. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  3. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  4. 【Lintcode】1522. Search in a Sorted Array of Unknown Size

    题目地址: https://www.lintcode.com/problem/search-in-a-sorted-array-of-unknown-size/description 给定一个长度非常 ...

  5. 【Lintcode】1723. Shortest Path in a Grid with Obstacles Elimination

    题目地址: https://www.lintcode.com/problem/shortest-path-in-a-grid-with-obstacles-elimination/descriptio ...

  6. 【转】SharePoint 2010 Search Service -- 管理中心配置

    今天配置了一下SharePoint Search,怕忘,赶紧记下来. 我参考了国外的一篇文章:Search Configuration in SharePoint 2010 首先,要查看以下User  ...

  7. 【PAT】A1053 Path of Equal Weight

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  8. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  9. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

最新文章

  1. 5G空口存疑 革新还是4G演进?
  2. Swift SB 容器 Container View使用
  3. vue3.0以上关于打包后出现空白页和路由不起作用
  4. 用Red5搭建支持WEB播放的实时监控视频
  5. 五一黄金周之二:可以吃人的博物馆
  6. Python面向对象基础一
  7. 《学习OpenCV3》第10章 滤波与卷积
  8. 页面之间传输大量数据
  9. 10 行代码判定色*情*图片
  10. QQ群排名优化规则-学会后10分钟全国排名第一
  11. 计算机的音量找不到了,win7旗舰版64位系统右下角音量小喇叭图标不见了怎么找回...
  12. HTML(hiden控件 readonly disabled)(maxlength属性)(id属性重点)
  13. 用Python写一个植物大战僵尸
  14. systemc学习资料
  15. 计算机进制计算试题,计算机各种进制转换练习题(附答案).pdf
  16. PalmOS开发教程-13
  17. java从小白到工程师--基础篇(二)
  18. 阿里P8程序员征婚被群嘲:我年薪170万,找月薪1万的老婆,有错吗
  19. 计算机学院电脑技能讲座,作曲系举办“电脑使用那些事——计算机操作技能实战”讲座...
  20. ZZ:OpenSSL中产生RSA KEY以及读取PEM文件的问题

热门文章

  1. Bambook 稍后阅读 V1.01 发布
  2. 【Python】办公篇-《极客Python之效率革命》(小甲鱼) 笔记一
  3. wi ndows怎么自动编页,15个小技巧,让我的Windows电脑更好用了!
  4. 《天天数学》连载60:二月二十九日
  5. [CSP-J 2021] 网络连接
  6. 欧洲语言框架A1到C2,开言英语怎么样,学习过一段时间的效果评价
  7. PCB设计软件-入门
  8. docker启动jenkins部署springboot到tomcat(集成:企业微信和邮件通知)
  9. emui11优先体验鸿蒙系统,华为EMUI 11正式发布,可优先体验鸿蒙系统
  10. JAVA电商 B2B2C商城系统 多用户商城系统 直播带货 新零售商城 o2o商城 电子商务 拼团商城 分销商城 直播商城 短视频商城 springcloud商城 spring cloud商城