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
        

 

posted @ 2019-03-17 00:43  周洋  阅读(182)  评论(0编辑  收藏  举报