题目如下:

We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.

We start at bus stop S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.

Example:
Input:
routes = [[1, 2, 7], [3, 6, 7]]
S = 1
T = 6
Output: 2
Explanation:
The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.

Note:

  • 1 <= routes.length <= 500.
  • 1 <= routes[i].length <= 500.
  • 0 <= routes[i][j] < 10 ^ 6.

解题思路:对于至少有两条公交线路经过的站点,称为换乘站。如果起点和终点不属于同一公交线路,那么必定要经过换乘站换乘另外的线路。所以对于所有的站点来说,有效的站点只有起点、终点、换乘站。过滤无效站点可以减少计算量,使用BFS即可求得结果。

代码如下:

class Solution(object):
def numBusesToDestination(self, routes, S, T):
"""
:type routes: List[List[int]]
:type S: int
:type T: int
:rtype: int
"""
transfer = {}
for route in routes:
for r in route:
transfer[r] = transfer.setdefault(r,0) + 1
key_list = []
for key in transfer.viewkeys():
key_list.append(key) for key in key_list:
if transfer[key] <= 1:del transfer[key] for i in range(len(routes)-1,-1,-1):
for j in range(len(routes[i])-1,-1,-1):
v = routes[i][j]
if v != S and v != T and v not in transfer:
del routes[i][j] #print routes visit = {}
visit[S] = 0
queue = [(S,0)]
while len(queue) > 0:
stop,count = queue.pop(0)
if stop == T:return count
for route in routes:
if stop not in route:
continue
for r in route:
if r not in visit or visit[r] > count+1:
queue.append((r,count+1))
visit[r] = count+1
return -1

【leetcode】815. Bus Routes的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. webdriervAPI(WebElement接口常用方法)

    from  selenium  import  webdriver driver  =  webdriver.Chorme() driver.get("http://www.baidu.co ...

  2. django中Queryset的删除问题、分页问题

    在开发选课界面时需要过滤掉已经选择过的课程,之前一直以为QuerySet是列表的结构,所以打算在判断之后使用list.remove()方法将已选的课程除掉,但在实际操作时,发现这么做并不行,原来Que ...

  3. mysql——插入、更新、删除数据(示例)

    插入数据 一.前提,新建表: ), sname ), sage ), ssex ) ); select * from student; 二.多种方式插入数据: ','zhaolei','1990-01 ...

  4. sqlalchemy的join使用

    ——.先看mysql的join链接方法 #encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Floa ...

  5. 格式化hdfs以及namnode没启动

    先stop-all.sh 删除hdfs-site.xml中的这两个目录 然后删除core-site.xml 中的这个目录 然后格式化hdfs hdfs namenode -format 即可启动成功 ...

  6. C++常见面试题:

    一.进程和线程的概念和区别 1.进程是系统进行资源调度的基本单位 2.线程是系统进行运算调度(处理器分配{CPU.内存})的基本单位 二.进程间的通信 进程间的通信共有5种: 1.管道 通常指无名管道 ...

  7. Ubuntu-虚拟机-忘记登陆密码

    前提 在我们使用Ubuntu虚拟机的过程中,偶尔会出现密码忘了的尴尬事情.里面又有重要资料,不能重新安装,这时我们要重置密码,接下来,让我们共同学习! 重启虚拟机-重启时按住 shift 会出现以下 ...

  8. CSP 命令行选项(201403-3)

    问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些选项.每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔.这些字符串中的第一个为该命令行工具的名字,由小写字母组成,你的程序不用 ...

  9. CentOS 7 yum安装LAMP,LNMP并搭建WordPress个人博客网站

    本次实验要进行的是在CentOS7.2,内核版本3.10.0-327.el7.x86_64的环境下搭建LAMP和LNMP,并在此之上做一个WordPress博客网站. [root@Shining ~] ...

  10. vscode 插件 配置

    第一页 第二页 第三页 settings.json配置 { "editor.fontSize": 20, "files.autoSave": "off ...