博客
关于我
LeetCode:1436.Destination City旅行终点站(C语言)
阅读量:392 次
发布时间:2019-03-05

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

为了找到旅游线路图的终点站,我们需要确定哪个城市没有任何出边连接到其他城市。终点站是所有起点城市之外的那个城市。

步骤如下:

  • 收集所有起点城市:遍历路径数组,记录每个路径的起点城市。
  • 收集所有出现过的城市:记录所有出现在路径中的城市。
  • 确定终点城市:检查所有城市,找出不在起点城市集合中的那个,返回它作为终点站。
  • 代码实现:

    def destCity(paths):    # 收集所有起点城市    start_set = {path[0] for path in paths}    # 收集所有出现过的城市    all_cities = {city for path in paths for city in path}    # 遍历所有城市,找到不在起点集合中的终点城市    for city in all_cities:        if city not in start_set:            return city    # 根据题目,至少有一个终点,所以无需处理无返回的情况

    示例测试:

    • 示例1:输入:paths = [["London","New York"], ["New York","Lima"], ["Lima","Sao Paulo"]]输出:"Sao Paulo"

    • 示例2:输入:paths = [["B","C"], ["D","B"], ["C","A"]]输出:"A"

    • 示例3:输入:paths = [["A","Z"]]输出:"Z"

    通过这种方法,我们可以准确地找到旅游线路图的终点站。

    转载地址:http://dadzz.baihongyu.com/

    你可能感兴趣的文章
    Oracle 11g 单实例安装文档
    查看>>
    Oracle 11g 操作ASM权限问题
    查看>>
    Oracle 11g 数据类型
    查看>>
    Oracle 11g 编译使用BBED
    查看>>
    oracle 11g 静默安装
    查看>>
    Oracle 11gR2学习之二(创建数据库及OEM管理篇)
    查看>>
    Oracle 11gR2构建RAC之(2)--配置共享存储
    查看>>
    Oracle 11g中的snapshot standby特性
    查看>>
    Oracle 11g关闭用户连接审计
    查看>>
    Oracle 11g忘记sys、system、scott密码该这样修改!
    查看>>
    Oracle 11g数据库安装和卸载教程
    查看>>
    Oracle 11g数据库成功安装创建详细步骤
    查看>>
    Oracle 11g超详细安装步骤
    查看>>
    Oracle 12c中的MGMTDB
    查看>>
    Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
    查看>>
    Oracle 9i数据库管理教程
    查看>>
    ORACLE Active dataguard 一个latch: row cache objects BUG
    查看>>
    oracle avg、count、max、min、sum、having、any、all、nvl的用法
    查看>>
    Oracle BEQ方式连接配置
    查看>>
    oracle Blob保存方式,oracle 存储过程操作blob
    查看>>