当前位置:网站首页>1184. 公交站间的距离 : 简单模拟题
1184. 公交站间的距离 : 简单模拟题
2022-07-24 11:31: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 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- 链表——剑指offer面试题 02.07. 链表相交
- Lanqiao cup provincial training camp - commonly used STL
- 高频笔试题(蔚来)
- 08【AIO编程】
- 【10】 Teamwork and cross team collaboration
- Simply use MySQL index
- Talk about software testing - automated testing framework
- Ask n! How many zeros are there behind
- MySql的DDL和DML和DQL的基本语法
- Win10 icon turns white, recovery method
猜你喜欢

08【AIO编程】

What is the charm of CSDN members? What's the use of him?

链表——142. 环形链表 II

字符串——344.反转字符串

JMeter interface test steps - Installation Tutorial - script recording - concurrent test

JMeter if controller

【C】 Understanding C language variable scope and life cycle from memory

IT圈中的Bug的类型与历史
![MOS管 —— 快速复苏应用笔记(壹)[原理篇]](/img/a1/8427c9b1d0ea0cecce820816510045.png)
MOS管 —— 快速复苏应用笔记(壹)[原理篇]

Types and history of bugs in it circle
随机推荐
【10】 Teamwork and cross team collaboration
运算放大器 —— 快速复苏笔记[贰](应用篇)
HCIP MGRE实验 第三天
Text message verification of web crawler
【Golang】golang实现发送微信服务号模板消息
Win10 icon turns white, recovery method
[golang] golang实现截取字符串函数SubStr
【C】 Recursive and non recursive writing of binary tree traversal
String - 541. Reverse string II
运算放大器 —— 快速复苏笔记[壹](参数篇)
Shell script
Operational amplifier - Notes on rapid recovery [1] (parameters)
[golang] golang implements the post request to send form type data function
Jmeter-Runtime控制器
2022, the average salary of the soft tester, after reading it, I was instantly cool
Linked list - 142. Ring linked list II
HDU5667 Sequence
[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
【Golang】golang中time类型的before方法
[golang] deletion and emptying of map elements in golang