当前位置:网站首页>[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;
}
}边栏推荐
- My creation anniversary
- AI writes a poem
- 指定opencv非标准安装的版本
- CMD command enters MySQL times service name or command error (fool teaching)
- 杰理之按键发起配对【篇】
- Tp6 realize Commission ranking
- 时间工具类
- 索引总结(突击版本)
- Jerry's headphones with the same channel are not allowed to pair [article]
- The project manager's "eight interview questions" is equal to a meeting
猜你喜欢
随机推荐
2022.07.04
[RT thread env tool installation]
Ucloud is a basic cloud computing service provider
Key points of anti reptile: identifying reptiles
841. 字符串哈希
Redis master-slave and sentinel master-slave switchover are built step by step
UCloud是基础云计算服务提供商
Kirin Xin'an joins Ningxia commercial cipher Association
R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize the violin diagram, set the palette parameter to customize the filling color of violin diagrams at different
Mysql, sqlserver Oracle database connection mode
Flink并行度和Slot详解
杰理之关于 TWS 声道配置【篇】
小试牛刀之NunJucks模板引擎
Welcome to the markdown editor
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
【Confluence】JVM内存调整
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
Browse the purpose of point setting
tp6 实现佣金排行榜









