Python Solution O(n) initial and O(1) sumRange


  • 0
    Y

    the code as following, no need for save the nums as well.
    """

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        sum = 0
        self.sumList = [0]
        for i in range(len(nums)):
            sum += nums[i]
            self.sumList.append(sum)
        
    def sumRange(self, i, j):
        """
        :type i: int
        :type j: int
        :rtype: int
        """
        return self.sumList[j+1] - self.sumList[i]

Log in to reply
 

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