We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 6294b42 + 3cca84d commit 13832b0Copy full SHA for 13832b0
swift/0724-find-pivot-index.swift
@@ -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