当前位置:网站首页>[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
2022-07-07 19:54:00 【Jin huaixuan】
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/tvdfij
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java:
class Solution {
public Integer getArraysum(int[] arr){
int total = 0;
for(int i =0 ; i < arr.length;i++){
total += arr[i];
}
return total;
}
public int pivotIndex(int[] nums) {
// Prefixes and ideas : Calculate the elements in the array and total, Traverse from left to right , The current element is num[i], Determine whether the central subscript condition is :sum == total - num[i] - sum ;
int total = getArraysum(nums);
// Use sum Store left and
int sum = 0;
// Use i The pointer begins to traverse
for(int i =0 ; i<nums.length ;i++){
if(sum == (total - nums[i] - sum)){
return i;
}
sum += nums[i];
}
return -1;
}
}
边栏推荐
- Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记
- 【RT-Thread env 工具安装】
- Redis master-slave and sentinel master-slave switchover are built step by step
- Visual Studio 插件之CodeMaid自动整理代码
- “本真”是什么意思
- How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
- 实训九 网络服务的基本配置
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
- Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
- L1-027 rental (Lua)
猜你喜欢
模拟实现string类
Numpy——2. Shape of array
Matplotlib drawing 3D graphics
Make this crmeb single merchant wechat mall system popular, so easy to use!
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
杰理之关于 TWS 配对方式配置【篇】
8 CAS
Openeuler prize catching activities, to participate in?
编译器优化那些事儿(4):归纳变量
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
随机推荐
841. 字符串哈希
[confluence] JVM memory adjustment
IP tools
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
PMP practice once a day | don't get lost in the exam -7.7
开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
关于ssh登录时卡顿30s左右的问题调试处理
项目经理『面试八问』,看了等于会了
时间工具类
Solve the problem of remote rviz error reporting
R language dplyr package select function, group_ The by function, filter function and do function obtain the third largest value of a specific numerical data column in a specified level in a specified
Unable to link the remote redis server (solution 100%
位运算介绍
openEuler 有奖捉虫活动,来参与一下?
一锅乱炖,npm、yarn cnpm常用命令合集
编译器优化那些事儿(4):归纳变量
UCloud是基础云计算服务提供商
实训九 网络服务的基本配置
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
2022年投资哪个理财产品收益高?