題目要求:
https://leetcode-cn.com/problems/range-sum-query-immutable/
class NumArray:
def __init__(self, nums: List[int]):
self.dp = nums[:]
# self.dp[i]存儲0~i的子序列和
for i in range(1, len(self.dp)):
self.dp[i] += self.dp[i - 1]
def sumRange(self, i, j):
return self.dp[j] - self.dp[i - 1] if i > 0 else self.dp[j]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)