题目如下:

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. sql server不同排序规则的数据库间字段的比较

    不同的排序规则的字段是不能直接比较的.会提示:无法解决 equal to 操作的排序规则冲突.可以把字段强制转换一个排序规则,这样就能比较了.示例: ------------------------- ...

  2. Java基础(六)

    面向对象 概述 生活举例 代码体验 类与对象的关系 类的定义 根据类创建对象 对象的基本使用 练习:手机类与对象 内存图:一个对象 内存图:两个对象 内存图:同一个对象 局部变量与成员变量的区别 pr ...

  3. c++—简单的密码本实现

    主要实现功能有增删改查数据,本地读取保存. 数据存储设计 data.h data.cpp #pragma once #define 添加账户 1 #define 删除账户 2 #define 修改账户 ...

  4. linux shell脚本中使用expect(脚本打开新终端自动远程连接顺便输一点指令)(巨坑)

    放弃吧 我找了六个小时都没找到可以用的方案(指标题括号里的内容) 给个曲线救国的方法: 现把expect脚本写成一个文件 在另一个shell脚本中调用

  5. FFmpeg4.0笔记:采集桌面

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集桌面 void test_desktop() { boo ...

  6. BugkuCTF--域名解析(windows)

    这是这道题的题目,很简洁,flag获得的方法也告诉你了,就差把域名解析. 那么域名怎么解析呢.. 打开C:\Windows\System32\drivers\etc中的hosts文件(用记事本打开), ...

  7. Swoft 2.0.5 更新,新增高效秒级定时任务、异常管理组件

    什么是 Swoft ? Swoft 是一款基于 Swoole 扩展实现的 PHP 微服务协程框架.Swoft 能像 Go 一样,内置协程网络服务器及常用的协程客户端且常驻内存,不依赖传统的 PHP-F ...

  8. html重置模板

    新浪的初始化: html,body,ul,li,ol,dl,dd,dt,p,h1,h2,h3,h4,h5,h6,form,fieldset,legend,img { margin: 0; paddin ...

  9. python网络爬虫(12)去哪网酒店信息爬取

    目的意义 爬取某地的酒店价格信息,示例使用selenium在Firefox中的使用. 来源 少部分来源于书.python爬虫开发与项目实战 构造 本次使用简易的方案,模拟浏览器访问,然后输入字段,查找 ...

  10. oppo 手机不能连接appium,提示does not have permission android.permission.CLEAR_APP_USER_DATA to clear data

    1)增加配置项noReset=true 2)除了常见开发者选项中打开usb调试,同时还需要开启以下2项,然后重启手机即可