【leetcode】1184. Distance Between Bus Stops
题目如下:
A bus has
n
stops numbered from0
ton - 1
that form a circle. We know the distance between all pairs of neighboring stops wheredistance[i]
is the distance between the stops numberi
and(i + 1) % n
.The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance between the given
start
anddestination
stops.Example 1:
Input: distance = [1,2,3,4], start = 0, destination = 1
Output: 1
Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.Example 2:
Input: distance = [1,2,3,4], start = 0, destination = 2
Output: 3
Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.Example 3:
Input: distance = [1,2,3,4], start = 0, destination = 3
Output: 4
Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.Constraints:
1 <= n <= 10^4
distance.length == n
0 <= start, destination < n
0 <= distance[i] <= 10^4
解题思路:因为公交车的路线是一个环形,所以A到B的最短路径不是顺时针方向就是逆时针方向,比较哪个距离小即可。
代码如下:
class Solution(object):
def distanceBetweenBusStops(self, distance, start, destination):
"""
:type distance: List[int]
:type start: int
:type destination: int
:rtype: int
"""
val = [0]
amount = 0
for i in distance:
amount += i
val.append(amount)
dis = abs(val[destination] - val[start])
return min(dis,amount-dis)
【leetcode】1184. Distance Between Bus Stops的更多相关文章
- 【LeetCode】Hamming Distance
问题网址 https://leetcode.com/problems/hamming-distance/ 就是一个异或后,求1的位数的问题. 看到问题之后,首先困扰是: int能不能求异或?是不是要转 ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
随机推荐
- 浏览器访问ipv6站点(未绑定主机的ipv6站点)
我们在浏览器直接输入ipv6地址敲回车,一般情况下浏览器会跳转到搜索引擎进行搜索. 我们需要在浏览器器中输入: http://[::1] 或者 [::1]
- Shell编程、part3
本节内容 1. shell流程控制 2. for语句 3. while语句 4. break和continue语句 5. case语句 6. shell编程高级实战 shell流程控制 流程控制是改变 ...
- Hand on Machine Learning 第三章:分类器
1. 获取数据 使用MNIST数据集练习分类任务 from sklearn.datasets import fetch_mldata from scipy.io import loadmat mnis ...
- 【神经网络与深度学习】caffe静态链接库“Unknown layer type: Convolution (known types: )”和“ 磁盘空间不足”问题的解决办法
这一段时间把caffe在windows环境下编译了一下,tool里面的cpp全部编译成了exe.再用的时候有两个问题让我头疼了好长时间! 第一个问题 "db_lmdb.hpp:14] Che ...
- Spring JdbcTemplate 和 NamedParameterJdbcTemplate 使用
1.简单介绍 DAO层 的一般使用常见的是MyBatis 和 Hibernate,但是Hibernate是重量级的,而且学习成本较高,Mybatis 需要编写大量配置文件及接口文件,对于简单的项目应用 ...
- 【转帖】Spring Boot 为什么这么火?
Spring Boot 为什么这么火? 2019/06/03 http://www.ityouknow.com/springboot/2019/06/03/spring-boot-hot.html 没 ...
- 为webService添加Interceptor(拦截器)
今天写一个简单的拦截器,以webService接口为例: 背景:H5的一个项目,只要调用H5webService 接口下面的方法都会触发一个AuthorityInterceptor去验证是否调用类型是 ...
- poj 1061 青蛙的约会+拓展欧几里得+题解
青蛙的约会+拓展欧几里得+题解 纵有疾风起 题意 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出 ...
- CGAL 属性配置
libgmp-10.lib libmpfr-4.lib boost_system-vc120-mt-gd-1_63.lib D:\dev\CGAL-4.9\include D:\dev\CGAL-4. ...
- window下的php命令行输出汉字乱码处理
1. 在php的代码中加入 header("content-type:text/html;charset=gbk"); 2. 设置命令行的字体 在命令行上右击属性 字体 选择如下字 ...