23. Running Sum of 1D Array

EasyArrayArray

Given an array nums, return a new array result where result[i] = sum(nums[0] + nums[1] + ... + nums[i]) — the running (prefix) sum at each index.

Input: An integer array nums of length n.

Output: Integer array of length n — the running sum.

Examples

Example 1
Input: [1,2,3,4]
Output: [1,3,6,10]
Explanation: result[0]=1, result[1]=1+2=3, result[2]=1+2+3=6, result[3]=1+2+3+4=10.
Example 2
Input: [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Each step adds 1. Result = [1,2,3,4,5].
Example 3
Input: [3,1,2,10,1]
Output: [3,4,6,16,17]
Explanation: 3, 3+1=4, 4+2=6, 6+10=16, 16+1=17.

Constraints

Asked by

GoogleAmazonMicrosoftBloombergMeta
Solve this problem in the editor →