给定一个整数数组  nums,求出数组从索引 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点。

示例:

给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

说明:

  1. 你可以假设数组不可变。
  2. 会多次调用 sumRange 方法。
class NumArray:

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.data=nums

    def sumRange(self, i, j):
        """
        :type i: int
        :type j: int
        :rtype: int
        """
        return sum(self.data[i:j+1])
        


# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)

 

相关文章:

  • 2021-12-03
  • 2021-06-24
  • 2021-10-25
  • 2021-08-17
  • 2021-09-13
  • 2021-11-14
  • 2021-12-03
  • 2021-08-15
猜你喜欢
  • 2021-05-23
  • 2021-06-12
  • 2021-06-19
  • 2021-05-26
  • 2021-06-08
  • 2021-05-09
  • 2021-05-10
  • 2022-01-19
相关资源
相似解决方案