博客
关于我
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/

    你可能感兴趣的文章
    ViewHolder的改进写法
    查看>>
    Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
    查看>>
    org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
    查看>>
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>