当前位置:网站首页>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;
}
};
边栏推荐
- 【业务安全-04】万能用户名及万能密码实验
- OpenSSF安全计划:SBOM将驱动软件供应链安全
- Redis持久化
- [WUSTCTF2020]girlfriend
- Buuctf Misc
- Is there any discount for opening an account now? Is it safe to open an account online?
- What is the difference between the FAT32 and NTFS formats on the USB flash disk
- Integration of entry-level SSM framework based on XML configuration file
- AXI总线
- [xman2018 qualifying] pass
猜你喜欢
Deep understanding of bit operations
Redis持久化
[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
Axi bus
AXI总线
Debug tool
对半查找(折半查找)
深入理解位运算
[OS command injection] common OS command execution functions and OS command injection utilization examples and range experiments - based on DVWA range
【业务安全-01】业务安全概述及测试流程
随机推荐
Learning records of numpy Library
Awk concise tutorial
The global chip market may stagnate, and China's chip expansion accelerates to improve its self-sufficiency rate against the trend
enable_if
【高等数学】从法向量到第二类曲面积分
同花顺能开户炒股吗?安全吗?
Axi bus
Bidding announcement: Oracle all-in-one machine software and hardware maintenance project of Shanghai R & D Public Service Platform Management Center
[advanced MySQL] MTS master-slave synchronization principle and Practice Guide (7)
简析国内外电商的区别
crane:字典项与关联数据处理的新思路
Pytorch learning 1 (learning documents on the official website)
招标公告:上海市研发公共服务平台管理中心Oracle一体机软硬件维保项目
Li Kou's 81st biweekly match
Redis持久化
Completely solve the problem of Chinese garbled code in Web Engineering at one time
【业务安全03】密码找回业务安全以及接口参数账号修改实例(基于metinfov4.0平台)
Four characteristics of transactions
PostgreSQL 15新版本特性解读(含直播问答、PPT资料汇总)
jvm 性能调优、监控工具 -- jps、jstack、jmap、jhat、jstat、hprof