当前位置:网站首页>LeetCode:1184. 公交站间的距离————简单
LeetCode:1184. 公交站间的距离————简单
2022-07-26 04:04:00 【Kinght_123】

题目
1184. 公交站间的距离
环形公交路线上有 n 个站,按次序从 0 到 n - 1 进行编号。我们已知每一对相邻公交站之间的距离,distance[i] 表示编号为 i 的车站和编号为 (i + 1) % n 的车站之间的距离。
环线上的公交车都可以按顺时针和逆时针的方向行驶。
返回乘客从出发点 start 到目的地 destination 之间的最短距离。
示例 1:
输入:distance = [1,2,3,4], start = 0, destination = 1
输出:1
解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。
示例 2:
输入:distance = [1,2,3,4], start = 0, destination = 2
输出:3
解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。
示例 3:
输入:distance = [1,2,3,4], start = 0, destination = 3
输出:4
解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
提示:
- 1 <= n <= 10^4
- distance.length == n
- 0 <= start, destination < n
- 0 <= distance[i] <= 10^4
解题思路
- 一次遍历就行。
Code
class Solution:
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
if start > destination:
start = start ^ destination
destination = start ^ destination
start = start ^ destination
return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))
运行结果

边栏推荐
- 《opencv学习笔记》-- 霍夫变换
- 《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器
- Can't the container run? The Internet doesn't have to carry the blame
- KBPC1510-ASEMI大芯片15A整流桥KBPC1510
- Bracket nesting problem (recommended Collection)
- Redis如何实现持久化?详细讲解AOF触发机制及其优缺点,带你快速掌握AOF
- 基于移位寄存器的同步FIFO
- Acwing第 61 场周赛【完结】
- 微信小程序实现音乐播放器(5)
- A large factory developed and tested one, and strangled its neck with a mouse line
猜你喜欢

第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数

Communication protocol and message format between microservices

Working ideas of stability and high availability guarantee

Go Plus Security:一款Build Web3不可或缺的安全生态基础设施

Realization of online shopping mall system based on JSP

Dat of deep learning

Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop

容器跑不动?网络可不背锅

Introduction to UFS CLK gate

STM32 state machine programming example - full automatic washing machine (Part 2)
随机推荐
深度学习之DAT
Operator new, operator delete supplementary handouts
(translation) timing of website flow chart and user flow chart
PHP <=> 太空船运算符(组合比较符)
STM32状态机编程实例——全自动洗衣机(下)
[digital ic/fpga] Hot unique code detection
Acwing第 61 场周赛【完结】
E-commerce operator Xiaobai, how to get started quickly and learn data analysis?
[unity3d shader] character projection and reflection
Connect external MySQL databases in istio Service Grid
The PHP Eval () function can run a string as PHP code
[cloud native] talk about the understanding of the old message middleware ActiveMQ
【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
php 实现从1累加到100的算法
booking.com缤客上海面经
Pits encountered by sdl2 OpenGL
测试用例设计方法之——招式组合,因果判定
软考 系统架构设计师 简明教程 | 案例分析解题技巧
PHP installation spool supports dtls protocol
2022 Hangzhou Electric Multi school bowcraft