当前位置:网站首页>【剑指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;
}
}
边栏推荐
- How to buy bank financial products? Do you need a bank card?
- Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
- 开源OA开发平台:合同管理使用手册
- R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
- ASP. Net kindergarten chain management system source code
- Zhong Xuegao wants to remain innocent in the world
- L1-025 positive integer a+b (Lua)
- 吞吐量Throughout
- Numpy——axis
- 【Confluence】JVM内存调整
猜你喜欢
Kirin Xin'an joins Ningxia commercial cipher Association
谷歌seo外链Backlinks研究工具推荐
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
Zhong Xuegao wants to remain innocent in the world
[RT thread env tool installation]
CMD command enters MySQL times service name or command error (fool teaching)
2022如何评估与选择低代码开发平台?
8 CAS
How to estimate the value of "not selling pens" Chenguang?
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
随机推荐
Longest common prefix (leetcode question 14)
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
爬虫实战(七):爬王者英雄图片
PMP practice once a day | don't get lost in the exam -7.7
转置卷积理论解释(输入输出大小分析)
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
How to estimate the value of "not selling pens" Chenguang?
时间工具类
Interpretation of transpose convolution theory (input-output size analysis)
UCloud是基础云计算服务提供商
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
How to share the same storage among multiple kubernetes clusters
State mode - Unity (finite state machine)
最多可以参加的会议数目[贪心 + 优先队列]
Visual Studio 插件之CodeMaid自动整理代码
指定opencv非标准安装的版本
2022如何评估与选择低代码开发平台?
微信公众号OAuth2.0授权登录并显示用户信息
R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist