当前位置:网站首页>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的大小的时候,会自动索引到前头。
边栏推荐
猜你喜欢

大神详解开源 BUFF 增益攻略丨直播
![LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路

Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing

LVGL 8.2 Line

Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000

华为云数据库DDS产品深度赋能

产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现

Summary of common problems in development

UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
随机推荐
Partial modification - progressive development
How to handle exceptions in multithreading?
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
【学习笔记】拟阵
Deep learning 7 transformer series instance segmentation mask2former
numpy笔记
CentOS 6.3 下 PHP编译安装JSON模块报错解决
LVGL 8.2 LED
openresty 重定向
Luo Gu - some interesting questions
每周招聘|高级DBA年薪49+,机会越多,成功越近!
数据库函数的用法「建议收藏」
PXE网络
[learning notes] matroid
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
Redis 解决事务冲突之乐观锁和悲观锁
Flutter reports an error no mediaquery widget ancestor found
web聊天室实现
Comment configurer un accord
从0到1建设智能灰度数据体系:以vivo游戏中心为例