swift: add «724. Find Pivot Index»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-01 23:49:40 +02:00
parent 9bebfc43ba
commit 5307e7ef85
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,18 @@
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
}
}