leetcode刷题第724题(python):寻找数组中心索引。
这个要注意中心索引是0和数组中关于负数的处理。
原题:
我的解法:
class Solution(object):
def pivotIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
sums = sum(nums)
sum1 = 0
for i in range(len(nums)):
if sum1 * 2 + nums[i] == sums:
return i
sum1 += nums[i]
else:
return -1
运行结果: