当前位置:网站首页>[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;
}
}
边栏推荐
- Research and practice of super-resolution technology in the field of real-time audio and video
- IP tools
- ant desgin 多选
- Kirin Xin'an cloud platform is newly upgraded!
- 索引总结(突击版本)
- Jerry's headphones with the same channel are not allowed to pair [article]
- MySQL、sqlserver oracle数据库连接方式
- ASP. Net gymnasium integrated member management system source code, free sharing
- Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
- PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
猜你喜欢
PV static creation and dynamic creation
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
The project manager's "eight interview questions" is equal to a meeting
Nunjuks template engine
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
爬虫实战(七):爬王者英雄图片
9 atomic operation class 18 Rohan enhancement
随机推荐
杰理之快速配对,不支持取消配对【篇】
ASP. Net kindergarten chain management system source code
杰理之开机自动配对【篇】
Kirin Xin'an cloud platform is newly upgraded!
实训九 网络服务的基本配置
杰理之关于 TWS 声道配置【篇】
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
Tips and tricks of image segmentation summarized from 39 Kabul competitions
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
强化学习-学习笔记8 | Q-learning
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
R language uses ggplot2 function to visualize the histogram distribution of counting target variables that need to build Poisson regression model, and analyzes the feasibility of building Poisson regr
剑指 Offer II 013. 二维子矩阵的和
Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
最多可以参加的会议数目[贪心 + 优先队列]
MySQL、sqlserver oracle数据库连接方式
Solve the error reporting problem of rosdep
Solve the problem of remote rviz error reporting
L1-028 judging prime number (Lua)
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!