LeetCode题解(1436):旅行终点站-寻找循环的终点(Python)
发布日期:2021-06-29 19:56:11 浏览次数:2 分类:技术文章

本文共 579 字,大约阅读时间需要 1 分钟。

题目:(简单)

标签:数组

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N) 40ms (83.01%)
Ans 2 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N) 36ms (94.12%)
Ans 3 (Python)

解法一(双集合):

def destCity(self, paths: List[List[str]]) -> str:    maybe = set()    wrong = set()    for path in paths:        wrong.add(path[0])        maybe.add(path[1])    return (maybe - wrong).pop()

解法二(增加跳出循环,提高效率):

def destCity(self, paths: List[List[str]]) -> str:    paths = list(zip(*paths))    wrong = set(paths[0])    for path in paths[1]:        if path not in wrong:            return path

转载地址:https://dataartist.blog.csdn.net/article/details/107160233 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1441):用栈操作构建数组(Python)
下一篇:LeetCode题解(1431):拥有最多糖果的孩子(Python)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月12日 07时51分01秒