Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pivot index

# Time:  O(n)
# Space: O(1)

class Solution(object):
    def pivotIndex(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        total = sum(nums)
        left_sum = 0
        for i, num in enumerate(nums):
            if left_sum == (total-left_sum-num):
                return i
            left_sum += num
        return -1
Source by shareablecode.com #
 
PREVIOUS NEXT
Tagged: #pivot #index
ADD COMMENT
Topic
Name
3+8 =