Leetcode Note: Go - Find the Middle Index in Array

Find the Middle Index in Array - LeetCode
https://leetcode.com/problems/find-the-middle-index-in-array/

  • Go 言語で取り組んだメモ

回答

Golang 100% fastest clean code - Find the Middle Index in Array - LeetCode
https://leetcode.com/problems/find-the-middle-index-in-array/solutions/3156246/golang-100-fastest-clean-code/

func findMiddleIndex(nums []int) int {
    totalSum := 0
    for _,v := range nums {
        totalSum += v 
    }
    
    leftSum := 0
    for k, v := range nums {
        totalSum -= v
        if totalSum == leftSum { return k }
        leftSum += v
    }
  
  return -1
}