You are given an integer array nums and a list of queries, where queries[j] = [left_j, right_j] asks for the sum of the elements of nums from index left_j to index right_j, both ends inclusive.
Implement rangeSums(nums, queries). Return an array answer of the same length as queries, where answer[j] is the sum for the j-th query.
Examples
Example 1
Input: nums = [-2,0,3,-5,2,-1], queries = [[0,2],[2,5],[0,5]]
Output: [1,-1,-3]
Explanation:
- Indices 0..2 hold -2, 0 and 3, which sum to 1.
- Indices 2..5 hold 3, -5, 2 and -1, which sum to -1.
- Indices 0..5 hold every element, which sums to -3.
Example 2
Input: nums = [5], queries = [[0,0]]
Output: [5]
Explanation: The only query covers the single element.
Constraints
1≤nums.length≤104
−105≤nums[i]≤105
1≤queries.length≤104
queries[j].length==2
0≤leftj≤rightj<nums.length
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up