Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Implement the NumArray class:
NumArray(int[] nums)Initializes the object with the integer arraynums.int sumRange(int i, int j)Return the sum of the elements of thenumsarray in the range[i, j]inclusive (i.e.,sum(nums[i], nums[i + 1], ... , nums[j]))
Example 1:
1 | Input |
Constraints:
0 <= nums.length <= 104-105 <= nums[i] <= 1050 <= i <= j < nums.length- At most
104calls will be made tosumRange.
动态规划
储存一个数组accumulateSums, 保存从0到i的和. 计算的时候通过accumulateSums来返回结果
1 | class NumArray { |