Loading...
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.
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.
Input: nums = [5], queries = [[0,0]]
Output: [5]
Explanation: The only query covers the single element.
nums.length ≤104nums[i] ≤105queries.length ≤104queries[j].length ==2nums.lengthClick "Run" to test with sample cases or "Submit" to run all tests.