题目描述:
代码:
class Solution:
def pivotIndex(self, nums):
if len(nums) == 0:
return -1
if sum(nums[1:])==0:
return 0
total = sum(nums)
now = nums[0]*2;
for i in range(1, len(nums)-1):
if now + nums[i] == total:
return i
now+=nums[i]*2
if sum(nums[0:len(nums)-1])==0:
return len(nums)-1
return -1;
测试代码:
from solution import *
nums = [1, 7, 3, 6, 5, 6]
print(Solution().pivotIndex(nums))
运行结果: