当前位置:网站首页>[算法] 剑指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
}
测试
边栏推荐
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- NRF24L01 troubleshooting
- Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- FairyGUI循环列表
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
- Unity3D制作注册登录界面,并实现场景跳转
- idea中好用的快捷键
- Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
猜你喜欢
Teach you to release a DeNO module hand in hand
RTKLIB: demo5 b34f.1 vs b33
dosbox第一次使用
NovAtel 板卡OEM617D配置步骤记录
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Fabrication d'un sac à dos simple fairygui
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
idea中好用的快捷键
Matlab读取GNSS 观测值o文件代码示例
随机推荐
Minio file download problem - inputstream:closed
There is no red exclamation mark after SVN update
Pytorch: tensor operation (I) contiguous
[offer78]合并多个有序链表
Meanings and differences of PV, UV, IP, VV, CV
JS function promotion and declaration promotion of VaR variable
Unity场景跳转及退出
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
MySQL time, time zone, auto fill 0
MySQL performance tuning - dirty page refresh
Programming homework: educational administration management system (C language)
使用rtknavi进行RT-PPP测试
Flink late data processing (3)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
RTKLIB: demo5 b34f.1 vs b33
Esp8266 connect onenet (old mqtt mode)
idea中导包方法
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Get the position of the nth occurrence of the string