当前位置:网站首页>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)
边栏推荐
- 文件 - 05 下载文件:根据文件Id下载文件
- 【科普向】5G核心网架构和关键技术
- 那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
- shell之条件语句(test、if、case)
- 自动翻译软件-批量批量自动翻译软件推荐
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
- opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
- Project exercise - memorandum (add, delete, modify, check)
- 事务的四大特性
猜你喜欢
随机推荐
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
Conditional statements of shell (test, if, case)
HuffmanTree
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
HighTec 的安装与配置
Install and use uView
iOS大厂面试查漏补缺
Run the NPM will pop up to ask "how are you going to open this file?"
codec2 BlockPool:不可读库
快速傅里叶变换(FFT)
我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
机器学习反向传播的一些推导公式
第十七章:回溯探求指定入口的马步遍历,贪心无回溯探求马步遍历,递归探求nxm棋盘带障碍马步遍历
Install the gstreamer development dependency library to the project sysroot directory
批量免费文字翻译
DirectExchange交换机简单入门demo
(border-box) The difference between box model w3c and IE
自动翻译软件-批量批量自动翻译软件推荐
mysql的下载及安装使用









