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]
출처:
728x90