Range Sum Query - Immutable

prefix Sum数组cache就行了

class NumArray(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        n = len(nums)
        self.cumSum = [0] * (n + 1)
        for index, num in enumerate(nums):
            self.cumSum[index + 1] = self.cumSum[index] + num

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


# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)

results matching ""

    No results matching ""