table of Contents
topic
1480. The dynamic sum of a one-dimensional array
gives you an array 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.
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].
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].
Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]
prompt:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
Problem solution (Java)
class Solution {
public int[] runningSum(int[] nums)
{
//初始化数组
int[] run = new int[nums.length];
//给数组赋值
for(int index = 0;index < run.length;index++)
{
run[index] = getSum(index, nums);
}
return run;
}
public int getSum(int i, int[] nums)
{
int sum = 0;
for(int index = 0;index <= i;index++)
{
sum += nums[index];
}
return sum;
}
}