【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 ...
随机推荐
- mysql中基本的语句
操作字段: 添加字段 ALTER TABLE 表名 ADD 字段 varchar(20) COMMENT '别名'; 修改表字段的属性等(除了修改表名称) ALTER TABLE 表名 MODIFY ...
- Adobe出品(支持IOS,android,web调用)免费插件编辑图片
<head runat="server"><meta http-equiv="Content-Type" content="text ...
- 2019JAVA第十一次实验报告
#Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.11.22 评分等级 简易记事本 实验代码 package Domon10; import java.aw ...
- IM学习目录
1. 浅谈即时通讯 https://www.cnblogs.com/shoshana-kong/p/9724963.html 2. im即时通讯 https://www.cnblogs.com/sh ...
- 关于eclipse设置JRebel
版本:eclipse ee Version: 2018-09 (4.9.0) jrebel:最新2019-2 1.在eclipse->help->eclipse Marketplace 2 ...
- selenium与页面交互之二:webelement类的属性
webelement类的属性如下: element.size() 获取元素的大小 element.tag_name() 获取元素的HTML标签名称 element.text() 获取元素的文本 ...
- [.net core]8.中间件的概念
假设我们的中间件是这样的(可以自由排列, 扩展自定义中间件) logging负责记录请求/响应 staticFiles 负责响应 静态文件 MVC 负责响应 视图 当.net core web app ...
- The library 'libhostpolicy.dylib' required to execute the application was not found in
.NET Core应用程序需要runtimeconfig.json文件.此JSON文件配置运行时的选项.没有runtimeconfig.json文件,这将失败. > dotnet Program ...
- 实现 RSA 算法之 C 语言实现(第二章)(老物)
第二章 如何实现应用RSA算法 趁着白天在自家店里的闲暇时间来写写第二章了,假设记住了第一章的各种定理之后,我们又该如何实现RSA密码的加密解密呢?也懒得废话了,直接进入正题吧. 先回顾几个知识点: ...
- css中新增的属性calc()可以计算使用
什么是calc: calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定元素的长度.可以使用calc()给元素的border.margin.pading.font-s ...