-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path724.py
More file actions
38 lines (34 loc) · 938 Bytes
/
724.py
File metadata and controls
38 lines (34 loc) · 938 Bytes
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
# class Solution(object):
# def pivotIndex(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# if not nums:
# return -1
# len_n = len(nums)
# for i in range(len_n):
# left, right = sum(nums[0:i]), sum(nums[i + 1:])
# if left == right:
# return i
# return -1
class Solution(object):
def pivotIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return -1
len_n = len(nums)
total = sum(nums)
left = 0
for i in range(len_n):
left = left + nums[i - 1] if i > 0 else left + 0
right = total - nums[i] - left
if left == right:
return i
return -1
if __name__ == '__main__':
n = [-1, -1, -1, 0, 1, 1]
print(Solution().pivotIndex(n))