Easy to understand Python Solution O(n) runtime O(n) space


  • 2
    T
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.nums = nums
        self.sumList = []
        sum = 0
        for index in range(len(nums)):
            sum += nums[index]
            (self.sumList).append(sum)
    
    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        return self.sumList[j] - self.sumList[i] + self.nums[i]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.