Skip to content

Commit 13832b0

Browse files
authored
Merge pull request neetcode-gh#2231 from itopstack/main
Create 0724-find-pivot-index.swift
2 parents 6294b42 + 3cca84d commit 13832b0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

swift/0724-find-pivot-index.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
func pivotIndex(_ nums: [Int]) -> Int {
6+
var nums = nums
7+
for i in 1..<nums.count {
8+
nums[i] += nums[i - 1]
9+
}
10+
11+
for i in 0..<nums.count {
12+
let left = i == 0 ? 0 : nums[i - 1]
13+
let right = i == nums.count - 1 ? 0 : nums[nums.count - 1] - nums[i]
14+
if left == right {
15+
return i
16+
}
17+
}
18+
19+
return -1
20+
}
21+
}

0 commit comments

Comments
 (0)