Skip to content

Commit 283b7d4

Browse files
committed
Just make a commit[not completion].
1 parent 4ab088e commit 283b7d4

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
Given an array of integers nums, calculate the pivot index of this array.
3+
4+
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
5+
6+
If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
7+
8+
Return the leftmost pivot index. If no such index exists, return -1.
9+
10+
11+
12+
Example 1:
13+
Input: nums = [1,7,3,6,5,6]
14+
Output: 3
15+
Explanation:
16+
The pivot index is 3.
17+
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
18+
Right sum = nums[4] + nums[5] = 5 + 6 = 11
19+
20+
Example 2:
21+
Input: nums = [1,2,3]
22+
Output: -1
23+
Explanation:
24+
There is no index that satisfies the conditions in the problem statement.
25+
26+
Example 3:
27+
Input: nums = [2,1,-1]
28+
Output: 0
29+
Explanation:
30+
The pivot index is 0.
31+
Left sum = 0 (no elements to the left of index 0)
32+
Right sum = nums[1] + nums[2] = 1 + -1 = 0
33+
34+
35+
Constraints:
36+
- 1 <= nums.length <= 10^4
37+
- -1000 <= nums[i] <= 1000
38+
*/
39+
class Solution {
40+
func pivotIndex(_ nums: [Int]) -> Int {
41+
if nums.count == 1 { return -1 }
42+
var middle = nums.count / 2
43+
var leftSum = nums[0..<middle].reduce(0, +)
44+
var rightSum = nums[(middle + 1)...(nums.count - 1)].reduce(0, +)
45+
var leftForward = false
46+
var rightForward = false
47+
while leftSum != rightSum {
48+
print("L --->", leftSum, "R --->", rightSum)
49+
if leftSum > rightSum {
50+
if middle > 0 && rightForward == false {
51+
middle -= 1
52+
leftForward = true
53+
} else {
54+
return -1
55+
}
56+
} else {
57+
if middle < nums.count - 1 && leftForward == false {
58+
middle += 1
59+
rightForward = true
60+
} else {
61+
return -1
62+
}
63+
}
64+
65+
if middle > 0 && middle < nums.count - 1 {
66+
leftSum = nums[0..<middle].reduce(0, +)
67+
rightSum = nums[(middle + 1)...(nums.count - 1)].reduce(0, +)
68+
} else {
69+
if middle == 0 {
70+
leftSum = 0
71+
rightSum = nums[(middle + 1)...(nums.count - 1)].reduce(0, +)
72+
} else if middle == nums.count - 1 {
73+
leftSum = nums[0..<middle].reduce(0, +)
74+
rightSum = 0
75+
}
76+
}
77+
}
78+
return middle
79+
}
80+
}
81+
82+
let s = Solution()
83+
let r = s.pivotIndex([-1,-1,-1,-1,-1,0])
84+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/724.Find Pivot Index.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)