当前位置:网站首页>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的大小的时候,会自动索引到前头。
边栏推荐
- 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?
- Redis哨兵模式实现一主二从三哨兵
- Is it safe to open an account online for stock speculation? Will you be cheated.
- 内存管理总结
- Memory management summary
- %s格式符
- Redis publish and subscribe
- 深度学习 神经网络案例(手写数字识别)
- How to match chords
- Ali was laid off employees, looking for a job n day, headhunters came bad news
猜你喜欢
Halo effect - who says that those with light on their heads are heroes
5G电视难成竞争优势,视频资源成中国广电最后武器
numpy笔记
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
暑期复习,一定要避免踩这些坑!
TechSmith Camtasia studio 2022.0.2 screen recording software
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
随机推荐
ES6 modularization
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
Redis的4种缓存模式分享
Halo effect - who says that those with light on their heads are heroes
They are all talking about Devops. Do you really understand it?
Redis publish and subscribe
浮点数如何与0进行比较?
从0到1建设智能灰度数据体系:以vivo游戏中心为例
LVLG 8.2 circular scrolling animation of a label
近一亿美元失窃,Horizon跨链桥被攻击事件分析
Ffprobe common commands
How to handle exceptions in multithreading?
openresty 重定向
EventBridge 在 SaaS 企业集成领域的探索与实践
selenium 浏览器(2)
%s格式符
flutter 报错 No MediaQuery widget ancestor found.
智能客服赛道:网易七鱼、微洱科技打法迥异
内存管理总结
When synchronized encounters this thing, there is a big hole, pay attention!