当前位置:网站首页>LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
2022-07-04 15:13:00 【TianChao lobster】
LeetCode 1184. The distance between bus stops —vector Clockwise and counterclockwise .
Title address : https://leetcode.cn/problems/distance-between-bus-stops/
Problem solving :
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);
}
};
Topic notes
The key point of the topic is to give a vector, A start and end , Calculate the cumulative sum respectively , Take the minimum . How to put vector The index is divided into two parts by the following statement :
(start+i)%n
(destination+i)%n
One accumulates from the beginning , Then on vector Size redundancy of , Until we reach the end . One Accumulate from the end , Also on the vector Size redundancy of , Until we reach the starting point . This remainder is very clever to put a vector Divided into two parts , Whether the end point is greater than the starting point , And greater than vector When it comes to the size of , Will automatically index to the front .
边栏推荐
- 音视频技术开发周刊 | 252
- AI做题水平已超过CS博士?
- Kubernets pod exists finalizers are always in terminating state
- 输入宽度!
- Helix swarm Chinese package is released, and perforce further improves the user experience in China
- 进制形式
- Intelligent customer service track: Netease Qiyu and Weier technology play different ways
- 科研漫画 | 联系到被试后还需要做什么?
- Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
- Halcon knowledge: NCC_ Model template matching
猜你喜欢
内存管理总结
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Summer Review, we must avoid stepping on these holes!
Introduction to modern control theory + understanding
How to handle exceptions in multithreading?
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
深度学习 网络正则化
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
一篇文章搞懂Go语言中的Context
随机推荐
输入宽度!
C1 certification learning notes 3 -- Web Foundation
Implementation of web chat room
Is BigDecimal safe to calculate the amount? Look at these five pits~~
一篇文章学会GO语言中的变量
Memory management summary
Optimization method of deep learning neural network
They are all talking about Devops. Do you really understand it?
Introduction to modern control theory + understanding
开源人张亮的 17 年成长路线,热爱才能坚持
深度学习 神经网络的优化方法
Enter the width!
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Openresty current limiting
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
Preliminary exploration of flask: WSGI
%s格式符
每周招聘|高级DBA年薪49+,机会越多,成功越近!
5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
openresty 重定向