当前位置:网站首页>1184. 公交站间的距离 : 简单模拟题
1184. 公交站间的距离 : 简单模拟题
2022-07-24 10:42:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 1184. 公交站间的距离 ,难度为 简单。
Tag : 「模拟」
环形公交路线上有 个站,按次序从 到 进行编号。我们已知每一对相邻公交站之间的距离, 表示编号为 的车站和编号为 (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。
提示:
模拟
根据题意进行模拟即可。
用 i 和 j 分别代表往前和往后走的指针,a 和 b 分别统计两种走法的总成本。
Java 代码:
class Solution {
public int distanceBetweenBusStops(int[] dist, int s, int t) {
int n = dist.length, i = s, j = s, a = 0, b = 0;
while (i != t) {
a += dist[i];
if (++i == n) i = 0;
}
while (j != t) {
if (--j < 0) j = n - 1;
b += dist[j];
}
return Math.min(a, b);
}
}
TypeScript 代码:
function distanceBetweenBusStops(dist: number[], s: number, t: number): number {
let n = dist.length, i = s, j = s, a = 0, b = 0
while (i != t) {
a += dist[i]
if (++i == n) i = 0
}
while (j != t) {
if (--j < 0) j = n - 1
b += dist[j]
}
return Math.min(a, b)
};
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.1184 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
本文由 mdnice 多平台发布
边栏推荐
- Sentinel three flow control modes
- 零基础学习CANoe Panel(4)——按钮(Button )
- After the QT program minimizes the tray, a msgbox pops up. Click OK and the program exits. The problem is solved
- MySQL - 唯一索引
- Protocol Bible - talk about ports and quads
- Daily three questions 7.22
- Binary search method
- ECCV 2022 | Tsinghua proposes the first transformer to embed spectral sparsity
- Qt创建应用程序托盘及相关功能
- How to gracefully realize idempotency and distributed current limiting of distributed interfaces (glory Collection Edition)
猜你喜欢

Rtklib source code, RTK difference calculation, rtkpos and replos function process sorting

Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire

零基础学习CANoe Panel(7)—— 开关/显示控件(Input/Output Box )

Distributed transaction processing scheme big PK!

5个最佳WordPress广告插件

Arduino + AD9833 波形发生器

Disk storage chain B-tree and b+ tree

Sentinel 流量控制快速入门

机器学习小试(10)使用Qt与Tensorflow创建CNN/FNN测试环境

What is NFT? How to develop NFT system?
随机推荐
Disk storage chain B-tree and b+ tree
Is it safe to open an online stock account?
零基础学习CANoe Panel(6)—— 开关/显示控件(Switch/Indicator)
MySQL - 全文索引
Distributed lock implementation scheme (glory collection version)
跨平台音频播放库
JSON tutorial [easy to understand]
ECCV 2022 | 清华提出首个嵌入光谱稀疏性的Transformer
js函数调用下载文件链接
题解——Leetcode题库第283题
零基础学习CANoe Panel(8)—— 数据/文本编辑控件(Hex/Text Editor )
Cookie sessionstorage localstorage differences
Qt应用程序防止多开,即单例运行
Rtklib source code, RTK difference calculation, rtkpos and replos function process sorting
Sentinel three flow control modes
Daily three questions 7.21
A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]
[dish of learning notes dog learning C] detailed operator
Onpropertychange event "suggestions collection"
火山引擎:开放字节跳动同款AI基建,一套系统解决多重训练任务