mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
swift: add «724. Find Pivot Index»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
9bebfc43ba
commit
5307e7ef85
1 changed files with 18 additions and 0 deletions
18
swift/find-pivot-index.swift
Normal file
18
swift/find-pivot-index.swift
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue