当前位置:网站首页>2022.07.24_每日一题
2022.07.24_每日一题
2022-07-31 06:07:00 【诺.い】
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^4distance.length == n0 <= start, destination < n0 <= distance[i] <= 10^4
- 数组
coding
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int len = distance.length;
int len1 = 0;
int len2 = 0;
if (start > destination) {
start = start ^ destination;
destination = start ^ destination;
start = start ^ destination;
}
for (int i = start; i < destination; i++) {
len1 += distance[i];
}
for (int i = destination; i < len + start; i++) {
len2 += distance[i % len];
}
return Math.min(len1, len2);
}
}
//leetcode submit region end(Prohibit modification and deletion)
边栏推荐
- Log4net 思维导图
- Gradle剔除依赖演示
- 外贸网站优化-外贸网站优化教程-外贸网站优化软件
- 强化学习科研知识必备(数据库、期刊、会议、牛人)
- Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
- 我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
- 高并发与多线程之间的难点对比(容易混淆)
- 【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
- Bulk free text translation
- 浅层了解欧拉函数
猜你喜欢

【C语言项目合集】这十个入门必备练手项目,让C语言对你来说不再难学!

《白帽子说Web安全》思维导图

Some derivation formulas for machine learning backpropagation

tidyverse笔记——dplyr包

文件 - 05 下载文件:根据文件Id下载文件

外贸网站优化-外贸网站优化教程-外贸网站优化软件

文件 - 04 下载文件: 根据文件下载链接下载文件

【解决】npm ERR A complete log of this run can be found in npm ERR

Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record

Analysis of pseudo-classes and pseudo-elements
随机推荐
文件 - 05 下载文件:根据文件Id下载文件
Bulk free text translation
【微服务】(十六)—— 分布式事务Seata
PCB抄板
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
DDL+DML+DQL
Koa框架的基本使用
360 push-360 push tool-360 batch push tool
【云原生】-Docker容器迁移Oracle到MySQL
Web浏览器工作流程解析
Kubernetes调度
MySql的安装配置超详细教程与简单的建库建表方法
【Star项目】小帽飞机大战(七)
Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
nohup principle
深度学习通信领域相关经典论文、数据集整理分享
Gradle剔除依赖演示
gstreamer的caps event和new_segment event
SQL Server Datetime2数据类型
机器学习反向传播的一些推导公式