303. Range Sum Query - Immutable(数组区间和)

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

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

 

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

 

求区间 i ~ j 的和,可以转换为 sum[j + 1] - sum[i],其中 sum[i] 为 0 ~ i - 1 的和。

时间复杂度:o(n)                  空间复杂度:o(n)

 

posted on 2019-03-12 23:46  shaer  阅读(98)  评论(0编辑  收藏  举报

导航