当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 12: The sum of the left and right subarrays is equal
subject 1:
Ideas 1:
// Ideas :
//1. Record the total number sum
//2. Traversal element calculation sum - nums[i] - leftSum == leftSum ,return i
Code
func pivotIndex(nums []int) int {
//start: 13.16 end: 13.22
// Ideas :
//1. Record the total number sum
//2. Traversal element calculation sum - nums[i] - leftSum == leftSum ,return i
// Processing parameters
if len(nums) == 0 {
return -1
}
//1. Record the total number sum
sum := 0
for i := 0; i < len(nums); i++{
sum += nums[i]
}
//2. Traverse the element to find the center point
leftSum := 0
for i := 0; i < len(nums); i++{
//(1) Judge
if sum - nums[i] - leftSum == leftSum{
return i
}
// Fail to judge
leftSum += nums[i]
}
return -1
}
test
边栏推荐
- Combination of fairygui check box and progress bar
- Fabrication of fairygui simple Backpack
- In 2020, the average salary of IT industry exceeded 170000, ranking first
- (课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
- idea中好用的快捷键
- Get the position of the nth occurrence of the string
- SSD technical features
- MySQL shutdown is slow
- Unity scene jump and exit
猜你喜欢
FairyGUI按钮动效的混用
音乐播放(Toggle && PlayerPrefs)
[算法] 剑指offer2 golang 面试题10:和为k的子数组
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
Programming homework: educational administration management system (C language)
(1) Introduction Guide to R language - the first step of data analysis
FairyGUI摇杆
In 2020, the average salary of IT industry exceeded 170000, ranking first
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
随机推荐
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Derivation of logistic regression theory
基于rtklib源码进行片上移植的思路分享
dosbox第一次使用
idea中导包方法
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
Latex learning
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
微信小程序开发心得
FairyGUI循環列錶
wsl常用命令
Mixed use of fairygui button dynamics
[算法] 剑指offer2 golang 面试题1:整数除法
Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
How to add music playback function to Arduino project
[Offer29] 排序的循环链表
FairyGUI条子家族(滚动条,滑动条,进度条)
SSD technical features
雇佣收银员【差分约束】