题目如下:

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. 系统的可用性用平均无故障时间( MTTF)

    计算机系统的可用性用平均无故障时间( MTTF)来度量,即计算机系统平均能够正常运行多长时间,才发生一次故障.系统的可用性越高,平均无故障时间越长. 可维护性用平均维修时间(MTTR)来度量,即系统发 ...

  2. VBNET线程和委托20191223

    1.每个程序有一个主线程,如果一个循环处于主线程中,程序在较长的循环,将出现“不响应”的情况. 线程在System.Threading中.线程创建可专用于一个功能块(方法.函数), 线程的开始用Sta ...

  3. RPC基本原理

    RPC非常重要,很多人面试的时候都挂在了这个地方!你要是还不懂RPC是什么?他的基本原理是什么?你一定要把下边的内容记起来!好好研究一下!特别是文中给出的一张关于RPC的基本流程图,重点中的重点,Du ...

  4. 手把手教你用 Strace 诊断问题

    早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人,十有八九会建议你用 strace 挂上去看看,不过当你挂上去了,看着满屏翻滚的 ...

  5. 02-Zookeeper介绍及安装

    1 Zookeeper介绍 ZooKeeper是为分布式应用所设计的高可用.高性能且一致的开源协调服务,它提供了一项基本服务:分布式锁服务.分布式应用可以基于它实现更高级的服务,实现诸如同步服务.配置 ...

  6. Centos 安装Pycharm 并移动到桌面。

    版权声明:版权所有.未经同意不得转发,装载 https://blog.csdn.net/limingyue0312/article/details/81805826 1.下载pycharm软件包 网页 ...

  7. 2018.07.17【省赛模拟】模拟B组 比赛总结

    题目 [GDKOI2003]最大公共子串 [题目描述] 从一个给定的串中删去(不一定连续地删去)0个或0个以上的字符,剩下的字符按原来的顺序组成的串是该串的字串.例如:"", &q ...

  8. CSS3鼠标悬停翻转按钮

    在线演示 本地下载

  9. Spring Boot全局支持CORS(跨源请求)

    import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet. ...

  10. springMvc接受单个文件,多个文件,多组文件

    web端 <form id="iconForm" enctype="multipart/form-data"></form> JS:通过 ...