📜  EquilibriumIndex - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:04.159000             🧑  作者: Mango

代码示例1
func findEquilibriumIndex(in array: [Int]) -> Int? {
  var leftSum = 0
  var rightSum = array.reduce(0, combine: +)


  for (index, value) in array.enumerated() {
    rightSum -= value

    if leftSum == rightSum {
        return index
    }

    leftSum += value
  }

  return nil
}

let sampleArray = [-7, 1, 5, 2, -4, 3, 0]

findEquilibriumIndex(in: sampleArray)