pydata

Keep Looking, Don't Settle

leetcode 303. Range Sum Query - Immutable

303. Range Sum Query - Immutable

这是一道easy的题目。

class NumArray(object):
    def __init__(self, nums):
        self.nums = nums     

    def sumRange(self, i, j):
        return sum(self.nums[i:(j+1)])
def sunRange(nums, i, j):
    sum = 0
    for x in range(i, j +1):
        sum += nums[x]
    return sum

def sunRange(nums, i, j):
    sum[0] = nums[0]
    for x in range(1, len(nums)):
        sum[x] = sum[x - 1] + nums[x]
    return sum[j] - sum[i - 1]