当前位置:网站首页>LeetCode 1184. 公交站间的距离 ---vector顺逆时针
LeetCode 1184. 公交站间的距离 ---vector顺逆时针
2022-07-04 14:00:00 【Tianchao龙虾】
LeetCode 1184. 公交站间的距离 —vector顺逆时针。
题目地址: https://leetcode.cn/problems/distance-between-bus-stops/
题目解法:
class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
int result_clockwise=0, result_counterclockwise=0;
int size=distance.size();
for(int i=0;(start+i)%size!=destination;++i)
result_clockwise+=distance[(start+i)%size];
for(int i=0;(destination+i)%size!=start;++i)
result_counterclockwise+=distance[(destination+i)%size];
return min(result_clockwise,result_counterclockwise);
}
};
题目笔记
题目重点是给出了一个vector,一个起点和终点,分别计算累计的和,取最小。怎么样把vector索引分成两个部分依靠下面语句:
(start+i)%n
(destination+i)%n
一个从开始累加,然后对vector的大小取余,直至到达终点。一个 从结束开始累加,也对vector的大小取余,直到到达起点。 这个取余就很巧妙的将一个vector分成两个部分,不管终点是否大于起点,且大于vector的大小的时候,会自动索引到前头。
边栏推荐
- 31年前的Beyond演唱会,是如何超清修复的?
- LVLG 8.2 circular scrolling animation of a label
- Guitar Pro 8win10 latest guitar learning / score / creation
- ES6 modularization
- Halo effect - who says that those with light on their heads are heroes
- 信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
- I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
- Is it safe to open an account online for stock speculation? Will you be cheated.
- Introduction to modern control theory + understanding
- LVGL 8.2 List
猜你喜欢
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
numpy笔记
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
[C language] Pointer written test questions
一篇文章搞懂Go语言中的Context
关于FPGA底层资源的细节问题
音视频技术开发周刊 | 252
Summary of common problems in development
IO流:节点流和处理流详细归纳。
随机推荐
Expose Ali's salary and position level
十六进制
Guitar Pro 8win10 latest guitar learning / score / creation
IO流:节点流和处理流详细归纳。
局部修改-渐进型开发
selenium 元素交互
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
Redis哨兵模式实现一主二从三哨兵
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
LVGL 8.2 Line
Preliminary exploration of flask: WSGI
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
内存管理总结
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
Optimization method of deep learning neural network
大神详解开源 BUFF 增益攻略丨直播
进制形式
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
Redis 發布和訂閱