这次用的还是土方法,没有用高大上的线段树,竟然还是过了。。。
class NumArray(object):
def __init__(self, nums):
"""
initialize your data structure here.
:type nums: List[int]
"""
self.items = nums
self.sum1 = sum(self.items)
def update(self, i, val):
"""
:type i: int
:type val: int
:rtype: int
"""
self.sum1 = self.sum1 - self.items[i] + val
self.items[i] = val
def sumRange(self, i, j):
"""
sum of elements nums[i..j], inclusive.
:type i: int
:type j: int
:rtype: int
"""
a = 0
if j - i >= len(self.items) / 2:
return self.sum1 - sum(self.items[:i]) - sum(self.items[j + 1:])
else:
return sum(self.items[i: j + 1])