当前位置:网站首页>Leetcode 1991. Find the middle position of the array (brute force enumeration)
Leetcode 1991. Find the middle position of the array (brute force enumeration)
2022-06-10 10:34:00 【I'm not xiaohaiwa~~~~】

I'll give you a subscript from 0 The starting array of integers nums , Please find Leftmost In the middle of middleIndex ( That is, the one with the smallest subscript in all possible middle positions ).
In the middle middleIndex Is to meet nums[0] + nums[1] + … + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + … + nums[nums.length-1] Array subscript of .
If middleIndex == 0 , The sum of the left part is defined as 0 . Allied , If middleIndex == nums.length - 1 , The sum of the right part is defined as 0 .
Please return to meet the above conditions Leftmost Of middleIndex , If there is no such middle position , Please return -1 .
Example 1:
Input :nums = [2,3,-1,8,4]
Output :3
explain :
Subscript 3 The sum of the previous numbers is :2 + 3 + -1 = 4
Subscript 3 The following numbers and are :4 = 4
Example 2:
Input :nums = [1,-1,4]
Output :2
explain :
Subscript 2 The sum of the previous numbers is :1 + -1 = 0
Subscript 2 The following numbers and are :0
Example 3:
Input :nums = [2,5]
Output :-1
explain :
There are no qualified middleIndex .
Example 4:
Input :nums = [1]
Output :0
explain :
Subscript 0 The sum of the previous numbers is :0
Subscript 0 The following numbers and are :0
Tips :
- 1 <= nums.length <= 100
- -1000 <= nums[i] <= 1000
Code:
class Solution {
public:
int findMiddleIndex(vector<int>& nums) {
int left=0;
int right=0;
for(int i=0;i<nums.size();i++)
{
if(i==0)
{
left=0;
right=accumulate(nums.begin()+i+1,nums.end(),0);
if(left==right)
return i;
}
if(i==nums.size()-1)
{
right=0;
left=accumulate(nums.begin(),nums.end()-1,0);
if(left==right)
return i;
}
left=accumulate(nums.begin(),nums.begin()+i,0);
right=accumulate(nums.begin()+i+1,nums.end(),0);
if(left==right)
return i;
}
return -1;
}
};
边栏推荐
- stm32 printf乱码
- 学术讲座: 多标签主动学习之 MASP
- 6、 Observer mode
- Academic lecture: masp for multi label active learning
- 仿淘宝商品详情,下拉切换到图片详情,上拉切换到图文简介
- 2022 examination questions and answers of labor worker post skills (labor worker)
- Market development details of NFT casting trading platform
- fcpx插件:PremiumVFX Animation Presets(动画循环预设) v1.0.1特别版
- 一个独特的简历生成器,开源了!
- phpstrom 將項目上傳碼雲
猜你喜欢

基于分层自监督学习将视觉Transformer扩展到千兆像素图像

Neo hacker song's award-winning list has been announced. Who owns tens of thousands of dollars of gold?

Axure add drop-down menu linkage

Introduction to development technology of multi-target application on SAP cloud platform
常用Shell命令 - 02 压缩和解压缩

Xcode8.3.2 automatic packaging script

62. 不同路径-动态规划

62. different paths - Dynamic Planning

【云原生利器之Cilium】什么是Cilium

Print: Entry, ':CFBundleIdentifier', Does Not Exist
随机推荐
Concurrent asyncio asynchronous programming
Today, 19:30 | graphics special session - Gao Lin, teacher team of Institute of computing technology, Chinese Academy of Sciences
“大写意花鸟画宗师李苦禅先生”重磅数字藏品全网首发
Axure pop-up settings
dried food! Training method of machinetranslation model based on mask label smoothing
Multithreading implementation
Install the latest version of cocoapods tutorial
8、 Chain mode
【先楫HPM6750测评】RT-Thread开发环境搭建和Hello World
PV操作每日一题-橘子苹果问题(高阶版变式)
Detailed steps for installing mysql+django under mac
【云原生利器之Cilium】什么是Cilium
Mixer: an indispensable component of video conference recording
[high concurrency] about optimistic lock and pessimistic lock, the interviewer of ant financial asked me these questions!!
"Great freehand flower and bird painting master Mr. likuchan" blockbuster digital collection launched in the whole network
Some problems in using message queue service in thinkphp6
2023王道C语言训练营(线索二叉树)
MySQL - operation database
已经过去多长时间的方式显示时间
[730. statistics of different palindrome subsequences]