当前位置:网站首页>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的大小的时候,会自动索引到前头。
边栏推荐
- UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- 【学习笔记】拟阵
- 基于MAX31865的温度控制系统
- EventBridge 在 SaaS 企业集成领域的探索与实践
- 关于FPGA底层资源的细节问题
- Deep learning neural network case (handwritten digit recognition)
- 小数,指数
- 信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
- Ali was laid off employees, looking for a job n day, headhunters came bad news
猜你喜欢
随机推荐
C language course design questions
Openresty current limiting
大神详解开源 BUFF 增益攻略丨直播
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
Is BigDecimal safe to calculate the amount? Look at these five pits~~
[C language] Pointer written test questions
go-zero微服务实战系列(九、极致优化秒杀性能)
Exploration and practice of eventbridge in the field of SaaS enterprise integration
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
Helix swarm Chinese package is released, and perforce further improves the user experience in China
Ranking list of databases in July: mongodb and Oracle scores fell the most
Ffprobe common commands
重排数组
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
函数计算异步任务能力介绍 - 任务触发去重
leecode学习笔记-约瑟夫问题
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
LVGL 8.2 keyboard
Leecode learning notes - Joseph problem
EventBridge 在 SaaS 企业集成领域的探索与实践








