【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 ...
随机推荐
- beego框架学习(三) -orm的使用
2 3 4 5 6 7 8 9 10 11 目前beego-orm支持的数据有: - MySQL:https://github.com/go-sql-driver/mysql - PostgreSQL ...
- VTK中,定义imread()函数,读取“.vts"文件
在本程序中,定义了一个imread()函数,用于读取“.vts”文件,第一个参数为文件路径,第二个参数为输出对象,可以输出为tkStructuredGrid型对象,也可以输出为vtkActor型对象. ...
- Doker部署Jmeter(一) 目标服务器部署Jmeter监控容器
用jmeter插件监控服务器性能之前也有提到:https://www.cnblogs.com/betterbb/p/11285022.html 这里主要记录一下docker上的部署,所需的3个插件可以 ...
- Python学习之并发基础知识
8 并发编程 8.1 基础知识 8.1.1 操作系统的定义 操作系统是存在于硬件与软件之间,管理.协调.调度软件与硬件的交互. 资源管理解决物理资源数量不足和合理分配资源这两个问题, 通俗来说,操作系 ...
- Java应用监控利器JMX
啥是 JMX? The Java Management Extensions (JMX) API is a standard API for management and monitoring of ...
- C++ 11的右值引用
目录 一.问题导入 二.右值和右值引用 2.1 左值(lvalue)和右值(rvalue) 2.2 左值引用和右值引用 总结 参考资料 C++11 引入了 std::move 语义.右值引用.移动构造 ...
- layer最大话.最小化.还原回调方法
layer.open({ type: 1, title: ‘在线调试‘, content: ‘这里是内容‘, ...
- 一些常用的字符串函数(CLR函数)
原代码来自:东莞--小小大神 使用 --聚合函数 SELECT father_key,dbo.String_Agg(department_name) FROM dbo.b_department GRO ...
- selenium之京东商品爬虫
#今日目标 **selenium之京东商品爬虫** 自动打开京东首页,并输入你要搜索的东西,进入界面进行爬取信息 ``` from selenium import webdriver import t ...
- python3的一些文件操作的脚手架
用python把原来的脚本重构了一下,其中写了文件操作的一些函数,如下: import os import shutil import hashlib import stat #查找文件夹中的某个文件 ...


