Range Sum Query - Immutable
303.Range Sum Query - Immutable
Given an integer array nums, handle multiple queries of the following type:
- Calculate the sum of the elements of
numsbetween indicesleftandrightinclusive whereleft <= right.
Implement the NumArray class:
NumArray(int[] nums)Initializes the object with the integer arraynums.int sumRange(int left, int right)Returns the sum of the elements ofnumsbetween indicesleftandrightinclusive (i.e.nums[left] + nums[left + 1] + ... + nums[right]).
Example 1:
1 | Input |
时间复杂度:O(n2)
空间复杂度:O(n)
1 | class NumArray { |
前缀和:
时间复杂度:O(n)
空间复杂度:O(n)
1 | class NumArray { |