mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-04 23:02:31 +00:00
添加Go语言题解
This commit is contained in:
@@ -107,3 +107,23 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func pivotIndex(nums []int) int {
|
||||
presum := 0
|
||||
for _, num := range nums {
|
||||
presum += num
|
||||
}
|
||||
var leftsum int
|
||||
for i, num := range nums {
|
||||
// 比较左半和右半是否相同
|
||||
if presum - leftsum - num == leftsum {
|
||||
return i
|
||||
}
|
||||
leftsum += num
|
||||
}
|
||||
return -1
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user