303. Range Sum Query - Immutable

题目:
https://leetcode.com/problems/range-sum-query-immutable/

tag : DP
难度 : Easy

sum(i, j) = nums[i] j = i
sum(i, j) = sum[i,j-1] + nums[j] j > i

Python代码

class NumArray(object):
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.sums = nums
        for i in range(1, len(self.sums)):
            self.sums[i] = self.sums[i-1] + self.sums[i]




    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        if i == 0 :
            return self.sums[j]
        else :
            return self.sums[j] - self.sums[i-1]
    原文作者:oo上海
    原文地址: https://www.jianshu.com/p/29d9f7ebdf5e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞