forked from yuyongwei/Algorithms-In-Swift
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindPivotIndex.swift
More file actions
38 lines (28 loc) · 1 KB
/
Copy pathfindPivotIndex.swift
File metadata and controls
38 lines (28 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// findPivotIndex.swift
//
//
// Created by Dong, Anyuan (133) on 2019/4/5.
//
import Foundation
/*
Given an array of integers nums, write a method that returns the "pivot" index of this array.
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
https://leetcode.com/articles/find-pivot-index/
*/
class Solution {
func pivotIndex(_ nums: [Int]) -> Int {
guard nums.count > 1 else { return nums.isEmpty ? -1 : 0 }
let sum = nums.reduce(0, +)
if sum - nums[0] == 0 { return 0 }
var leftTotal = 0
for i in 1..<nums.count {
leftTotal += nums[i-1]
if (sum - leftTotal - nums[i]) == leftTotal {
return i;
}
}
return -1
}
}