当前位置:网站首页>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 .
边栏推荐
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- PXE网络
- 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
- openresty 限流
- Summer Review, we must avoid stepping on these holes!
- 对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- PLC模拟量输入 模拟量转换FC S_ITR (CODESYS平台)
- 基于MAX31865的温度控制系统
- 深度学习 网络正则化
- Redis的4种缓存模式分享
猜你喜欢

内存管理总结

When synchronized encounters this thing, there is a big hole, pay attention!

這幾年爆火的智能物聯網(AIoT),到底前景如何?

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?

Kubernets pod exists finalizers are always in terminating state

flutter 报错 No MediaQuery widget ancestor found.

直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构

MySQL learning notes - data type (numeric type)

Helix Swarm中文包发布,Perforce进一步提升中国用户体验

Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
随机推荐
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
CentOS 6.3 下 PHP编译安装JSON模块报错解决
怎么判断外盘期货平台正规,资金安全?
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
IO flow: node flow and processing flow are summarized in detail.
进制形式
一篇文章搞懂Go语言中的Context
从0到1建设智能灰度数据体系:以vivo游戏中心为例
IO流:节点流和处理流详细归纳。
Weibo and Huya advance into interest communities: different paths for peers
函数计算异步任务能力介绍 - 任务触发去重
Implementation of web chat room
Partial modification - progressive development
Unity预制件Prefab Day04
Optimization method of deep learning neural network
How to rapidly deploy application software under SaaS
重排数组
hexadecimal
一篇文章学会GO语言中的变量