1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/swift/find-pivot-index.swift

19 lines
341 B
Swift
Raw Normal View History

class Solution {
func pivotIndex(_ nums: [Int]) -> Int {
var fromLeft = 0
var fromRight = nums.reduce(0, +)
for (i, x) in nums.enumerated() {
fromRight -= x
if fromLeft == fromRight {
return i
}
fromLeft += x
}
return -1
}
}