1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/swift/find-pivot-index.swift
Matej Focko 5307e7ef85
swift: add «724. Find Pivot Index»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-01 23:49:40 +02:00

18 lines
341 B
Swift

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
}
}