当前位置:网站首页>[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
边栏推荐
- FairyGUI按钮动效的混用
- [leetcode622]设计循环队列
- JUC forkjoin and completable future
- 雇佣收银员【差分约束】
- Latex learning
- Naive Bayesian theory derivation
- FGUI工程打包发布&导入Unity&将UI显示出来的方式
- [Offer18]删除链表的节点
- [算法] 劍指offer2 golang 面試題2:二進制加法
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
猜你喜欢
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
FGUI工程打包发布&导入Unity&将UI显示出来的方式
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
闇の連鎖(LCA+树上差分)
基本Dos命令
NovAtel 板卡OEM617D配置步骤记录
dosbox第一次使用
Combination of fairygui check box and progress bar
341. Flatten nested list iterator
随机推荐
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
基于rtklib源码进行片上移植的思路分享
FGUI工程打包发布&导入Unity&将UI显示出来的方式
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Prove the time complexity of heap sorting
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
[leetcode19] delete the penultimate node in the linked list
Naive Bayesian theory derivation
FairyGUI按钮动效的混用
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Servlet
How to reduce the shutdown time of InnoDB database?
KF UD分解之UD分解基础篇【1】
What are the functions and features of helm or terrain
Fairygui gain buff value change display
What are the advantages of using SQL in Excel VBA
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
FairyGUI循环列表
燕山大学校园网自动登录问题解决方案
Database course design: college educational administration management system (including code)