Leetcode 724. Find Pivot Index

prefix sum

class Solution(object):
    def pivotIndex(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        p,size=[0],len(nums)
        if size==0:
            return -1
        for val in nums:
            p.append(p[-1]+val)
        for i in range(size):
            if p[i+1]==p[size]-p[i]:
                return i
        return -1
        
原文地址:https://www.cnblogs.com/zywscq/p/10545138.html