Range sums
Write code · Medium · §3.2 Shifting and ranges
You are given a list of integers nums and a list of ranges queries. Each element [s, e] of queries is the range from nums[s] to nums[e] (both ends included).
Complete the function solution, which finds the sum of each range and returns the sums in a list, in the same order as queries.
Function
solution(nums, queries)
Constraints
- The length of
numsis from 1 to 1,000, and each element is an integer from -1,000 to 1,000. - The length of
queriesis from 1 to 1,000, and every range satisfies0 <= s <= e < len(nums).
Examples
| nums | queries | result |
|---|---|---|
[1, 2, 3, 4, 5] | [[1, 3], [0, 4]] | [9, 15] |
[10, -3, 7] | [[1, 1], [0, 1]] | [-3, 7] |
Starter code
def solution(nums, queries):
answer = []
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.