当前位置:网站首页>力扣 1184. 公交站间的距离
力扣 1184. 公交站间的距离
2022-07-24 21:52:00 【冷酷的摸鱼小将】
题目
环形公交路线上有 n 个站,按次序从 0 到 n - 1 进行编号。我们已知每一对相邻公交站之间的距离,distance[i] 表示编号为 i 的车站和编号为 (i + 1) % n 的车站之间的距离。
环线上的公交车都可以按顺时针和逆时针的方向行驶。
返回乘客从出发点 start 到目的地 destination 之间的最短距离。
示例

输入:distance = [1,2,3,4], start = 0, destination = 1
输出:1
解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。

输入:distance = [1,2,3,4], start = 0, destination = 2
输出:3
解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。

输入:distance = [1,2,3,4], start = 0, destination = 3
输出:4
解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/distance-between-bus-stops
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:模拟
Java实现
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
if (start > destination) {
int tmp = start;
start = destination;
destination = tmp;
}
int sum = 0, n = distance.length, flag = 0;
for (int i = 0; i < n; i++) {
sum += distance[i];
if (i >= start && i < destination) flag += distance[i];
}
return Math.min(flag, sum - flag);
}
}

边栏推荐
- Mathematical derivation in [pumpkin Book ml] (task4) neural network
- SQL语言的通用语法及分类(二)
- A compatible, smaller and easy-to-use web font API
- PCL点云处理之CSF布料模拟滤波(五十九)
- 从A76到A78——在变化中学习ARM微架构
- 实现redis哨兵,模拟master故障场景
- Principle of an automatic nine point calibration tool (including part of the source code)
- Random sampling and thinning of PCL point cloud processing randomsample (60)
- [which is better to use, apopost or apifox? Just read this!]
- CAD break command
猜你喜欢

Segment tree,,

Gradle学习集合整合

Archsummit: evolution of the underlying framework of cherished microservices

Kubernetes scheduling concept and workflow

Homework of the 20th week

工业物联网中的时序数据

【数据库学习】Redis 解析器&&单线程&&模型

Go+语言

Projection regularization of line point set in PCL point cloud processing (56)

Use of templates
随机推荐
图结构的实现,从点到边再到图
SQL语言的通用语法及分类(二)
Gradle 学习 ----Gradle 入门
Cell专刊|AI在蛋白结构、精准医疗、抗体疗法[综述]等的应用与未来预测
数据库之-元数据 DatabaseMetaData 初学
Gradle learning - integration of gradle and idea
QT learning vs creating QT items shows instances where object references are not set to objects
In the eyes of professionals in the robot industry, what is the current situation of the robot industry?
PCL点云处理之均匀采样抽稀(六十一)
Dialogue with celebrities: where are the opportunities and challenges in the second half when brands gather at the shuzang track?
A compatible, smaller and easy-to-use web font API
做好项目管理的10个关键点和5大措施
单调栈结构练习——子数组最小值的累加和
Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
集成Swagger 学习
"Fundamentals of program design" Chapter 10 function and program structure 6-13 divide and conquer method to solve the gold bullion problem (20 points)
[which is better to use, apopost or apifox? Just read this!]
AC automata
PCL点云处理之pcd文件转txt文件(单个或多个批量转换)(六十三)
What are the methods of knowledge map relation extraction