LintCode:图中两个点之间的路线
2016-05-12 14:50
363 查看
LintCode:图中两个点之间的路线
方法二:
Python
方法一:# Definition for a Directed graph node # class DirectedGraphNode: # def __init__(self, x): # self.label = x # self.neighbors = [] class Solution: """ @param graph: A list of Directed graph node @param s: the starting Directed graph node @param t: the terminal Directed graph node @return: a boolean value """ def hasRoute(self, graph, s, t): # write your code here if len(graph) == 0: return False if s == t: return True L = s.neighbors while len(L) != 0: s = L[0] if t in L: return True L.remove(s) L = list(set(L + s.neighbors)) return False
方法二:
# Definition for a Directed graph node # class DirectedGraphNode: # def __init__(self, x): # self.label = x # self.neighbors = [] class Solution: """ @param graph: A list of Directed graph node @param s: the starting Directed graph node @param t: the terminal Directed graph node @return: a boolean value """ def hasRoute(self, graph, s, t): # write your code here if len(graph) == 0: return False if s == t: return True L = s.neighbors ans = self.fun(L, s, t) return ans def fun(self, L, s, t): if s == t: return True if len(L) == 0: return False L += s.neighbors L = list(set(L)) s = L[0] L.remove(s) # print [node.label for node in L] return self.fun(L, s, t)
相关文章推荐
- Installation error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE的解决方法
- 图文详解如何快捷搭建LNMP服务环境
- 一句话说出你对云计算的理解
- JPanel重画方法
- [SPOJ BALNUM] Balanced Numbers
- js中定时器使用的小问题
- 单元测试的基本准则
- 对齐次坐标的理解
- 欢迎使用CSDN-markdown编辑器
- Android之十三图片的2D翻转
- Jquery 解决移动端onclick事件300ms延迟问题
- NoClassDefFoundError出现的一个原因
- apply plugin: 'java'原理
- javascript多线程--web workers实现线程间数据交互的小测试
- 解决socket服务器(chatserver)的问题
- 毛[文强]老师的一堂前端课程
- 关于神经网络学习的相关资源
- app被拒,官方提供了crash日志,需要解析获取到崩溃的具体位置(一)
- Android学习笔记
- c++对象内存模型【内存布局】