当前位置:网站首页>[算法] 剑指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
}
测试

边栏推荐
- Fairygui joystick
- GNSS定位精度指标计算
- Unity3D,阿里云服务器,平台配置
- Unity场景跳转及退出
- Detailed explanation of truncate usage
- [offer78]合并多个有序链表
- Teach you to release a DeNO module hand in hand
- Meanings and differences of PV, UV, IP, VV, CV
- VLSM variable length subnet mask partition tips
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
猜你喜欢

音乐播放(Toggle && PlayerPrefs)

(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart

Vulnhub target: hacknos_ PLAYER V1.1

FairyGUI簡單背包的制作

Database course design: college educational administration management system (including code)

数据库课程设计:高校教务管理系统(含代码)

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

Guided package method in idea

Excel导入,导出功能实现

FairyGUI简单背包的制作
随机推荐
Halcon knowledge: gray_ Tophat transform and bottom cap transform
(一)R语言入门指南——数据分析的第一步
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
MySQL replacement field part content
Unity3D制作注册登录界面,并实现场景跳转
FairyGUI增益BUFF数值改变的显示
Latex learning
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
idea中好用的快捷键
What is the maximum length of MySQL varchar field
【RTKLIB 2.4.3 b34 】版本更新简介一
[leetcode622]设计循环队列
如何给Arduino项目添加音乐播放功能
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
[leetcode19]删除链表中倒数第n个结点
单片机蓝牙无线烧录
FGUI工程打包发布&导入Unity&将UI显示出来的方式
Combination of fairygui check box and progress bar
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
What are the advantages of using SQL in Excel VBA