当前位置:网站首页>Leetcode 1184:公交站间的距离
Leetcode 1184:公交站间的距离
2022-07-26 07:06:00 【勇敢小姚】
题目描述:
环形公交路线上有 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。
解法一:迭代
思路:
Step1:用sum统计总的距离长度
Step2:用len统计由起始公交站到终点公交站之间的距离。假设len为顺时针的走法,那么sum-len的值即为逆时针的走法。
Step3:计算得到最小值即可。
代码:
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
//统计距离总长
int sum = 0;
for(int n : distance){
sum += n;
}
//保证start比destination小
int len = 0;
if(start > destination){
int temp = start;
start = destination;
destination = temp;
}
//找到从start到destination之间的距离,假设这个len就是顺时针走的,那么sum-len后的距离就是逆时针走的,然后求最小值即可
for(int i=start; i < destination; i++){
len += distance[i];
}
return Math.min(len, sum-len);
}
}解法二:
思路:
思路与解法一大同小异,不再赘述。
代码:
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int total = 0, s = 0, min = Math.min(start, destination), max = Math.max(start, destination);
for (int i = 0; i < distance.length; i++) {
total += distance[i];
if (min <= i && i < max) {
s += distance[i];
}
}
return Math.min(s, total - s);
}
}
边栏推荐
- Drools(2):Drools快速入门
- shape 和 size() 区别
- Multi-objective collaborative decision-making in supply chain
- 123123123
- QT监听socket事件,使用QSocketNotifier类
- NiO implementation
- PMP customs formula, don't hurry to recite it
- Kernel pwn 入门 (5)
- Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
- Question: can't download sh shellcheck Please install it manually and some commands of shell script
猜你喜欢
![[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design](/img/ba/87cd3b1600bcb6f2839e7bb093ff62.png)
[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design

Analysis of strong tennis cup 2021 PWN competition -- baby_ diary

Queue assistant | product update log in June 2022
![[database] CTE (common table expression)](/img/36/812026995f5d0b64d26f1667638010.png)
[database] CTE (common table expression)

LTS(Light-Task-Scheduler)
![[Star Project] small hat aircraft War (III)](/img/43/af4e923e901c1b2ad32b60c980dcdb.png)
[Star Project] small hat aircraft War (III)

哈夫曼编码原理

文件服务器FastDFS

浅谈eval与assert一句话木马执行区别

Press in and pop-up sequence of "Niuke | daily question" stack
随机推荐
【硬十宝典】——7.1【动态RAM】DDR硬件设计要点
LeetCode刷题1:题目分类
【Star项目】小帽飞机大战(二)
Can you learn fast and well with dual stream network? Harbin Institute of Technology & Microsoft proposed a distillation dual encoder model for visual language understanding, which can achieve fast an
Wechat applet - from entry to penetration
NPM command
【QT】详解 *.pro、*.pri、*.prf、*.prl文件
“蔚来杯“2022牛客暑期多校训练营1补题记录(ACDGIJ)
Do you want to restart the database to replace the license?
Drools(4):Drools基础语法(2)
Drools (3): drools basic syntax (1)
Drools(3):Drools基础语法(1)
An album has been released, from introductory practical demonstration to advanced live Q & A, playing with container service so easy~
文件服务器FastDFS
Do you know what "parts" MySQL contains?
NIO实现
Common programming shortcut keys of idea (take off after learning the operation)
Deep learning learning notes -- solve the problem of slow download of CONDA and pip
Introduce you to JVM from architecture
Shell programming

