freephys
2/10/2020 - 3:55 PM

303. Range Sum Query - Immutable

["NumArray","sumRange","sumRange","sumRange"] [[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]

class NumArray(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.accu = [0]
        for num in nums:
            self.accu += self.accu[-1]+num,
            #self.accu.append(self.accu[-1]+num)

    def sumRange(self, i, j):
        """
        :type i: int
        :type j: int
        :rtype: int
        """
        return self.accu[j+1]-self.accu[i]
		
class NumArray(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.lst = nums[:]


    def sumRange(self, i, j):
        """
        :type i: int
        :type j: int
        :rtype: int
        """
        return sum(self.lst[i:j+1])