当前位置:网站首页>2022.07.24_Daily Question
2022.07.24_Daily Question
2022-07-31 07:40:00 【No. い】
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)
边栏推荐
- 在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
- 【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
- 第十六章:构建n(5,7)阶素数幻方
- Moment.js common methods
- 【微服务】Nacos集群搭建以及加载文件配置
- LeetCode brush # 376 # Medium - swing sequence
- 中断及pendSV
- Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
- 双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
- 强化学习科研知识必备(数据库、期刊、会议、牛人)
猜你喜欢
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
PCB抄板
【科普向】5G核心网架构和关键技术
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
tidyverse笔记——dplyr包
【微服务】Nacos集群搭建以及加载文件配置
金融租赁业务
小实战项目之——吃货联盟订餐系统
简单谈谈Feign
随机推荐
【Star项目】小帽飞机大战(八)
强化学习科研知识必备(数据库、期刊、会议、牛人)
Kubernetes scheduling
基金投顾业务
DAY18: Xss Range Clearance Manual
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
DAY18:Xss 靶场通关手册
2022.07.14_每日一题
LeetCode brush # 376 # Medium - swing sequence
【并发编程】ReentrantLock的lock()方法源码分析
Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
How to choose a suitable UI component library in uni-app
DAY18:XSS 漏洞
中断及pendSV
DirectExchange交换机简单入门demo
R——避免使用 col=0
iOS大厂面试查漏补缺
《白帽子说Web安全》思维导图
03-SDRAM:写操作(突发)
自动翻译软件-批量批量自动翻译软件推荐