【leetcode】1184. Distance Between Bus Stops
题目如下:
A bus has
nstops numbered from0ton - 1that form a circle. We know the distance between all pairs of neighboring stops wheredistance[i]is the distance between the stops numberiand(i + 1) % n.The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance between the given
startanddestinationstops.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^4distance.length == n0 <= start, destination < n0 <= 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 ...
随机推荐
- cocos2dx基础篇(7) 触碰事件
cocos2dx游戏引擎的重点是在于移动设备的跨平台开发,而移动设备上的游戏大部分都是通过屏幕触碰来进行的.比如主菜单的按钮触碰,打飞机中飞机的触碰移动,都需要用到触碰操作.想一想之前讲的菜单按钮CC ...
- 意想不到的JavaScript(每日一题1)
问题: 答案: 解析:
- shell下的 awk/sed/grep/seq/tr
转自:实例手册 https://github.com/liquanzhou/ops_doc/blob/master/shell%E5%AE%9E%E4%BE%8B%E6%89%8B%E5%86%8C. ...
- 为 JS 的字符串,添加一个 format 的功能。
<script> String.prototype.format = function (kwargs) { var ret = this.replace(/\{(\w+)\}/g, fu ...
- equals与== 和toString方法
/** * equals()方法的使用 * * 1.java.lang.Object类中的equals()方法的定义: * * public boolean equals(Object obj) { ...
- numpy伪随机数的生成
numpy伪随机数的生成 normal函数 可以用normal来得到一个标准正态分布的4×4样本数组 >>> import numpy as np >>> samp ...
- Select 和Alert
Select 和Alert使用前都必须先导入 from selenium.webdriver.common.alert import Alert from selenium.webdriver.sup ...
- Python类函数调用:missing 1 required positional argument
在Python中,应该先对类进行实例化,然后在应用类.注意,实例化的过程是应该加括号的.
- --解决Lock wait timeout exceeded; try restarting transaction
--解决Lock wait timeout exceeded; try restarting transaction select * from information_schema.innodb_t ...
- MySQL数据库入门常用基础命令
MySQL数据库入门———常用基础命令 数据——公司的生命线,因此在大多数的互联网公司,都在使用开源的数据库产品,MySQL也因此关注度与使用率非常的高,所以做为运维的屌丝们,掌握它的一些基 ...


