当前位置:网站首页>【剑指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;
}
}边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
- 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
- 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
- 干货分享|DevExpress v22.1原版帮助文档下载集合
- State mode - Unity (finite state machine)
- Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
- Numpy——axis
- Interpretation of transpose convolution theory (input-output size analysis)
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- The project manager's "eight interview questions" is equal to a meeting
猜你喜欢

杰理之关于 TWS 配对方式配置【篇】

Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry

AD域组策略管理

Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)

编译原理 实验一:词法分析器的自动实现(Lex词法分析)

Numpy——2.数组的形状

CMD command enters MySQL times service name or command error (fool teaching)

PMP每日一练 | 考试不迷路-7.7

Research and practice of super-resolution technology in the field of real-time audio and video

2022.07.04
随机推荐
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
L1-028 judging prime number (Lua)
解决远程rviz报错问题
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
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
Specify the version of OpenCV non-standard installation
杰理之测试盒配置声道【篇】
tp6 实现佣金排行榜
项目经理『面试八问』,看了等于会了
8 CAS
Time tools
5billion, another master fund was born in Fujian
Solve the error reporting problem of rosdep
2022.07.05
LeetCode 515(C#)
解决rosdep的报错问题
干货分享|DevExpress v22.1原版帮助文档下载集合
My creation anniversary
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
Number - number (Lua)