Range Sum Queries
EasyPrefix Sum · Meta
You are given an integer array nums and a list of queries, where each query is [left, right] with 0 <= left <= right < nums.length.
For each query, compute the sum of nums[left] + nums[left + 1] + ... + nums[right] (both ends inclusive). Return the answers as a list in the same order as queries.
There can be many queries, so avoid re-adding the same elements for every query.
Example 1
- Input:
- nums = [3,-4,5,6], queries = [[1,2],[0,3],[3,3]]
- Output:
- [1,10,6]
- Explanation
nums[1..2]is -4 + 5 = 1, the whole array is 3 - 4 + 5 + 6 = 10, andnums[3..3]is just 6.
Example 2
- Input:
- nums = [2,2,2,2,2], queries = [[0,4],[1,3],[2,2]]
- Output:
- [10,6,2]
- Explanation
- The ranges cover 5, 3 and 1 elements, each worth 2, giving 10, 6 and 2.
Example 3
- Input:
- nums = [7], queries = []
- Output:
- []
- Explanation
- With no queries the answer list is empty.
Constraints
1 <= nums.length <= 10^4-10^5 <= nums[i] <= 10^50 <= queries.length <= 10^40 <= left <= right < nums.lengthfor every query