Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 10, 2023
1 parent c93d2cc commit 53d87c1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions leetcode/leetcode75/724.FindPivotIndex/findPivotIndex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
)

func pivotIndex(nums []int) int {
sum := 0
for i := 0; i < len(nums); i++ {
sum += nums[i]
}

leftSum, rightSum := 0, 0
for i := 0; i < len(nums); i++ {
rightSum = sum - nums[i] - leftSum
if leftSum == rightSum {
return i
}
leftSum += nums[i]
}

return -1
}

func pivotIndex1(nums []int) int {
prefix := make([]int, len(nums))
prefix[0] = nums[0]

for i := 1; i < len(nums); i++ {
prefix[i] = prefix[i-1] + nums[i]
}

for i := 0; i < len(nums); i++ {
if prefix[i] == prefix[len(nums)-1]-prefix[i]+nums[i] {
return i
}
}
return -1
}

func main() {
nums := []int{1, 7, 3, 6, 5, 6}
fmt.Println(pivotIndex(nums))
}

0 comments on commit 53d87c1

Please sign in to comment.