当前位置:网站首页>【剑指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;
}
}
边栏推荐
- 华南X99平台打鸡血教程
- Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
- The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an
- Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
- 指定opencv非标准安装的版本
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- [Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
- R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
- CMD command enters MySQL times service name or command error (fool teaching)
- 杰理之快速配对,不支持取消配对【篇】
猜你喜欢
爬虫实战(七):爬王者英雄图片
Matplotlib drawing 3D graphics
Make this crmeb single merchant wechat mall system popular, so easy to use!
转置卷积理论解释(输入输出大小分析)
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
Command mode - unity
CMD command enters MySQL times service name or command error (fool teaching)
RESTAPI 版本控制策略【eolink 翻译】
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
随机推荐
LeetCode 535(C#)
强化学习-学习笔记8 | Q-learning
2022.07.05
Redis master-slave and sentinel master-slave switchover are built step by step
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
# 欢迎使用Markdown编辑器
银行理财产品怎么买?需要办银行卡吗?
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
8 CAS
我的创作纪念日
How to share the same storage among multiple kubernetes clusters
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
Longest common prefix (leetcode question 14)
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
AI writes a poem
LeetCode1051(C#)
5billion, another master fund was born in Fujian
how to prove compiler‘s correctness