当前位置:网站首页>【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
2022-07-07 17:37:00 【瑾怀轩】
给你一个整数数组 nums ,请计算数组的 中心下标 。
数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。
如果中心下标位于数组最左端,那么左侧数之和视为 0 ,因为在下标的左侧不存在元素。这一点对于中心下标位于数组最右端同样适用。
如果数组有多个中心下标,应该返回 最靠近左边 的那一个。如果数组不存在中心下标,返回 -1 。
示例 1:
输入:nums = [1,7,3,6,5,6]
输出:3
解释:
中心下标是 3 。
左侧数之和 sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ,
右侧数之和 sum = nums[4] + nums[5] = 5 + 6 = 11 ,二者相等。
示例 2:
输入:nums = [1, 2, 3]
输出:-1
解释:
数组中不存在满足此条件的中心下标。
示例 3:
输入:nums = [2, 1, -1]
输出:0
解释:
中心下标是 0 。
左侧数之和 sum = 0 ,(下标 0 左侧不存在元素),
右侧数之和 sum = nums[1] + nums[2] = 1 + -1 = 0 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/tvdfij
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
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) {
//前缀和思想:计算数组中元素和total,从左至右遍历,当前元素为num[i], 判断是不是中心下标条件为:sum == total - num[i] - sum ;
int total = getArraysum(nums);
//使用sum存储左边和
int sum = 0;
//使用i指针开始遍历
for(int i =0 ; i<nums.length ;i++){
if(sum == (total - nums[i] - sum)){
return i;
}
sum += nums[i];
}
return -1;
}
}边栏推荐
- 时间工具类
- L1-025 positive integer a+b (Lua)
- Longest common prefix (leetcode question 14)
- [confluence] JVM memory adjustment
- The project manager's "eight interview questions" is equal to a meeting
- IP 工具类
- 注解。。。
- Zhong Xuegao wants to remain innocent in the world
- Business experience in virtual digital human
- Key points of anti reptile: identifying reptiles
猜你喜欢
随机推荐
The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
LeetCode 515(C#)
PMP每日一练 | 考试不迷路-7.7
小试牛刀之NunJucks模板引擎
[RT thread env tool installation]
9 atomic operation class 18 Rohan enhancement
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
Install mysql8 for Linux X ultra detailed graphic tutorial
Command mode - unity
Visual Studio 插件之CodeMaid自动整理代码
Numpy——axis
Redis master-slave and sentinel master-slave switchover are built step by step
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
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
LeetCode 535(C#)
String - string (Lua)
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
Ucloud is a basic cloud computing service provider









