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

边栏推荐
- [leetcode622] design circular queue
- Compile GDAL source code with nmake (win10, vs2022)
- Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
- Meanings and differences of PV, UV, IP, VV, CV
- FairyGUI复选框与进度条的组合使用
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- [Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
- FairyGUI人物状态弹窗
猜你喜欢

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

单片机蓝牙无线烧录

Single chip Bluetooth wireless burning

MySQL time, time zone, auto fill 0

NRF24L01 troubleshooting

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现

FairyGUI循环列表

MySQL shutdown is slow

Fabrication of fairygui simple Backpack

FairyGUI复选框与进度条的组合使用
随机推荐
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Meanings and differences of PV, UV, IP, VV, CV
Office提示您的许可证不是正版弹框解决
JS function promotion and declaration promotion of VaR variable
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
What is the maximum length of MySQL varchar field
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
单片机蓝牙无线烧录
Esp8266 connect onenet (old mqtt mode)
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Knowledge system of digital IT practitioners | software development methods -- agile
[899]有序队列
rtklib单点定位spp使用抗差估计遇到的问题及解决
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Solution to the problem of automatic login in Yanshan University Campus Network
C programming exercise
[leetcode19]删除链表中倒数第n个结点
How to add music playback function to Arduino project
Unity3d, Alibaba cloud server, platform configuration