当前位置:网站首页>Leetcode 724. Find the central subscript of the array (yes, once)
Leetcode 724. Find the central subscript of the array (yes, once)
2022-06-27 14:05:00 【I'm not xiaohaiwa~~~~】

Give you an array of integers nums , Please calculate the of the array Center subscript .
Array Center subscript Is a subscript of the array , The sum of all elements on the left is equal to the sum of all elements on the right .
If the central subscript is at the leftmost end of the array , Then the sum of the numbers on the left is regarded as 0 , Because there is no element to the left of the subscript . This also applies to the fact that the central subscript is at the rightmost end of the array .
If the array has multiple central subscripts , Should return to Closest to the left The one of . If the array does not have a central subscript , return -1 .
Example 1:
Input :nums = [1, 7, 3, 6, 5, 6]
Output :3
explain :
The central subscript is 3 .
The sum of the numbers on the left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ,
The sum of the numbers on the right sum = nums[4] + nums[5] = 5 + 6 = 11 , Two equal .
Example 2:
Input :nums = [1, 2, 3]
Output :-1
explain :
There is no central subscript in the array that satisfies this condition .
Example 3:
Input :nums = [2, 1, -1]
Output :0
explain :
The central subscript is 0 .
The sum of the numbers on the left sum = 0 ,( Subscript 0 There is no element on the left ),
The sum of the numbers on the right sum = nums[1] + nums[2] = 1 + -1 = 0 .
Tips :
- 1 <= nums.length <= 10^4
- -1000 <= nums[i] <= 1000
Code:
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int left=0;
int right=0;
for(int i=0;i<nums.size();i++)
{
if(i!=0)
left=accumulate(nums.begin(),nums.begin()+i,0);
right=accumulate(nums.begin()+i+1,nums.end(),0);
// cout<<left<<" "<<right<<endl;
if(left==right)
return i;
}
return -1;
}
};
边栏推荐
- Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance
- [WUSTCTF2020]girlfriend
- 【业务安全-02】业务数据安全测试及商品订购数量篡改实例
- Pytorch learning 3 (test training model)
- 深入理解位运算
- Semaphore of thread synchronization
- Dynamic Networks and Conditional Computation论文简读和代码合集
- Practice of constructing ten billion relationship knowledge map based on Nebula graph
- Calcul de la confidentialité Fate - Prévisions hors ligne
- AXI總線
猜你喜欢

Redis 主从复制、哨兵模式、Cluster集群

Make a ThreadLocal (source code) that everyone can understand
![[PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities](/img/19/9827a5e8becfc9d5bf2f51bddf803e.png)
[PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities

Getting to know cloud native security for the first time: the best guarantee in the cloud Era
![[microservices sentinel] hotspot rules | authorization rules | cluster flow control | machine list](/img/42/efebf981888704b3ad42d7d4404aa7.png)
[microservices sentinel] hotspot rules | authorization rules | cluster flow control | machine list

【业务安全03】密码找回业务安全以及接口参数账号修改实例(基于metinfov4.0平台)

Too many requests at once, and the database is in danger

Practice of constructing ten billion relationship knowledge map based on Nebula graph

请求一下子太多了,数据库危

初识云原生安全:云时代的最佳保障
随机推荐
The second part of the travel notes of C (Part II) structural thinking: Zen is stable; all four advocate structure
高效率取幂运算
[problem solving] which nodes are run in tensorflow?
简析国内外电商的区别
深入理解位运算
Implementing springboard agent through SSH port forwarding configuration
同花顺能开户炒股吗?安全吗?
In the past, domestic mobile phones were arrogant in pricing and threatened that consumers would like to buy or not, but now they have plummeted by 2000 for sale
Crane: a new way of dealing with dictionary items and associated data
buuctf misc 百里挑一
Yyds dry goods inventory solution sword finger offer: cut rope (advanced version)
每日3题(2):检查二进制字符串字段
Number of printouts (solved by recursive method)
Shell concise tutorial
线程同步之信号量
AXI總線
以前国产手机高傲定价扬言消费者爱买不买,现在猛降两千求售
enable_if
CMOS级电路分析
巧用redis实现点赞功能,它不比mysql香吗?