Description of the question:
https://leetcode.com/problems/range-sum-query-immutable/
My Code:
class NumArray:
def __init__(self, nums: List[int]):
self.sums = [0]*len(nums)
Sum = 0
for i, j in enumerate(nums):
Sum += j
self.sums[i] = Sum
def sumRange(self, i: int, j: int) -> int:
if i ==0 :
return self.sums[j]
else:
return self.sums[j]-self.sums[i-1]
Explanation: we store each sum into the object function OO.sums, then whenever we need to use it, we may be able to call this function and use it. This extra space used may save time from repeated calculation.