[LeetCode] 1480. Dynamic sum of one-dimensional array (C++)

1 topic description

Give you an array of nums. The calculation formula for the "dynamic sum" of the array is: runningSum[i] = sum(nums[0]…nums[i]).
Please return the dynamic sum of nums.

2 Example description

2.1 Example 1

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: The dynamic and calculation process is [1, 1+2, 1+2+3, 1+2+3+ 4].

2.2 Example 2

Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: The dynamic and calculation process is [1, 1+1, 1+1+1, 1+ 1+1+1, 1+1+1+1+1].

2.3 Example 3

Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]

3 Problem solving tips

1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6

4 Detailed source code (C++)

class Solution 
{
    
    
    public:
        vector<int> runningSum(vector<int>& nums) 
        {
    
    
            for (int i = 1 ; i < nums.size() ; i++)
            {
    
    
                nums[i] = nums[i-1] +nums[i];
            }
            return nums;
        }
};

Guess you like

Origin blog.csdn.net/Gyangxixi/article/details/113934937