码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Bus Routes 公交线路

时间:2019-01-20 10:34:35      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:ant   init   only   strategy   tin   tar   ber   his   dex   

 

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.
 

s

 

[LeetCode] Bus Routes 公交线路

标签:ant   init   only   strategy   tin   tar   ber   his   dex   

原文地址:https://www.cnblogs.com/grandyang/p/10293947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!