当前位置:网站首页>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)
边栏推荐
- 测试 思维导图
- 文件 - 03 下载文件:根据文件id获取下载链接
- 【第四章】详解Feign的实现原理
- 安装gstreamer开发依赖库到项目sysroot目录
- 快速傅里叶变换(FFT)
- Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
- (border-box) The difference between box model w3c and IE
- DirectExchange交换机简单入门demo
- 【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
- Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
猜你喜欢
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
【解决】npm ERR A complete log of this run can be found in npm ERR
How to use repeating-linear-gradient
LeetCode brush # 376 # Medium - swing sequence
Basic usage of Koa framework
把 VS Code 当游戏机
熟悉而陌生的新朋友——IAsyncDisposable
Obtaining server and client information
随机推荐
Install the gstreamer development dependency library to the project sysroot directory
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
【第四章】详解Feign的实现原理
关于求反三角函数的三角函数值
基于LSTM的诗词生成
线程唤醒机制
How to use repeating-linear-gradient
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
二叉树的还原(反序列化)
【Go】Go 语言切片(Slice)
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
HuffmanTree
SCI写作指南
高并发与多线程之间的难点对比(容易混淆)
nohup principle
Third-party library-store
多进程全局变量失效、变量共享问题
批量免费文字翻译