当前位置:网站首页>[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
题目1:
思路1:
//思路:
//1.记录总数 sum
//2.遍历元素计算 sum - nums[i] - leftSum == leftSum ,return i
代码
func pivotIndex(nums []int) int {
//start: 13.16 end: 13.22
//思路:
//1.记录总数 sum
//2.遍历元素计算 sum - nums[i] - leftSum == leftSum ,return i
//参数处理
if len(nums) == 0 {
return -1
}
//1.记录总数 sum
sum := 0
for i := 0; i < len(nums); i++{
sum += nums[i]
}
//2.遍历元素求中心点
leftSum := 0
for i := 0; i < len(nums); i++{
//(1) 判断
if sum - nums[i] - leftSum == leftSum{
return i
}
//判断失败
leftSum += nums[i]
}
return -1
}
测试
边栏推荐
- 程序设计大作业:教务管理系统(C语言)
- FairyGUI增益BUFF数值改变的显示
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- idea问题记录
- idea中导包方法
- Guided package method in idea
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- PRIDE-PPPAR源码解析
猜你喜欢
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
First use of dosbox
Office prompts that your license is not genuine pop-up box solution
NRF24L01故障排查
FairyGUI按钮动效的混用
Mysql database index
What are the advantages of using SQL in Excel VBA
RTKLIB: demo5 b34f.1 vs b33
Vulnhub target: hacknos_ PLAYER V1.1
idea中好用的快捷键
随机推荐
Theoretical derivation of support vector machine
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
[Offer29] 排序的循环链表
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
Detailed explanation of truncate usage
FGUI工程打包发布&导入Unity&将UI显示出来的方式
基本Dos命令
Liste des boucles de l'interface graphique de défaillance
Derivation of logistic regression theory
What are the advantages of using SQL in Excel VBA
rtklib单点定位spp使用抗差估计遇到的问题及解决
idea问题记录
SVN更新后不出现红色感叹号
Game 280 weekly
GNSS定位精度指标计算
Teach you to release a DeNO module hand in hand
[leetcode19]删除链表中倒数第n个结点
FairyGUI条子家族(滚动条,滑动条,进度条)
音乐播放(Toggle && PlayerPrefs)
JS function promotion and declaration promotion of VaR variable