Leetcode

303. Range Sum Query - Immutable

YGSEO 2020. 8. 16. 02:25
728x90
class NumArray(object):
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.sums = [0] * (len(nums) + 1)
        for i in xrange(len(nums)):
            self.sums[i+1] = self.sums[i] + nums[i]

    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        return self.sums[j+1] - self.sums[i]

출처:

leetcode.com/problems/range-sum-query-immutable/discuss/75319/Share-my-Python-solution-O(n)-for-init-and-O(1)-for-query

728x90