当前位置:网站首页>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^4
distance.length == n
0 <= start, destination < n
0 <= 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)
边栏推荐
- 【微服务】(十六)—— 分布式事务Seata
- 新瓶陈酒 --- 矩阵快速幂
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 从 Google 离职,前Go 语言负责人跳槽小公司
- 外贸网站优化-外贸网站优化教程-外贸网站优化软件
- 文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
- Some derivation formulas for machine learning backpropagation
- 服务器和客户端信息的获取
- 快速傅里叶变换(FFT)
- 【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
猜你喜欢
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
【第四章】详解Feign的实现原理
事务的传播机制
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
03-SDRAM:写操作(突发)
【微服务】(十六)—— 分布式事务Seata
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
Install and use uView
随机推荐
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
【编程题】【Scratch三级】2022.03 冬天下雪了
DirectExchange switch simple introduction demo
Install and use uView
Automatic translation software - batch batch automatic translation software recommendation
2022.7.29 数组
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
One of the small practical projects - food alliance ordering system
SQL Server Datetime2数据类型
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
Obtaining server and client information
安装gstreamer开发依赖库到项目sysroot目录
新瓶陈酒 --- 矩阵快速幂