
class NumArray1(object):
"""
store num from start to current position
use minus operation to get result
"""
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.sum_array = [0]
for i in range(len(nums)):
self.sum_array.append(nums[i] + self.sum_array[i])
print(self.sum_array)
def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
return self.sum_array[j+1] - self.sum_array[i]
class NumArray(object):
def __init__(self, nums):
self.nums = nums
for i in range(len(self.nums)):
if i == 0: continue
else:
self.nums[i] += self.nums[i-1]
def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
if i == 0:
return self.nums[j]
else:
return self.nums[j] - self.nums[i-1]