当前位置:网站首页>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的大小的时候,会自动索引到前头。
边栏推荐
- Ranking list of databases in July: mongodb and Oracle scores fell the most
- Width and alignment
- 找数字
- C1 certification learning notes 3 -- Web Foundation
- 如何搭建一支搞垮公司的技术团队?
- Quick introduction to automatic control principle + understanding
- Openresty current limiting
- 夜天之书 #53 Apache 开源社群的“石头汤”
- 微博、虎牙挺进兴趣社区:同行不同路
- 毕业季-个人总结
猜你喜欢
![Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding

Kubernets pod exists finalizers are always in terminating state

Redis publier et s'abonner

Redis 发布和订阅

Ffmpeg Visual Studio development (IV): audio decoding

Weibo and Huya advance into interest communities: different paths for peers

函数计算异步任务能力介绍 - 任务触发去重

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example

MP3是如何诞生的?

华为云数据库DDS产品深度赋能
随机推荐
关于FPGA底层资源的细节问题
[C language] Pointer written test questions
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
LVLG 8.2 circular scrolling animation of a label
进制形式
Preliminary exploration of flask: WSGI
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Halo effect - who says that those with light on their heads are heroes
C language course design questions
近一亿美元失窃,Horizon跨链桥被攻击事件分析
The performance of major mainstream programming languages is PK, and the results are unexpected
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
LVGL 8.2 Draw label with gradient color
Ffmpeg Visual Studio development (IV): audio decoding
Summer Review, we must avoid stepping on these holes!
unity update 协程_Unity 协程的原理
Ranking list of databases in July: mongodb and Oracle scores fell the most
每周招聘|高级DBA年薪49+,机会越多,成功越近!
自动控制原理快速入门+理解
Redis的4种缓存模式分享