1480. Running Sum of 1d Array

1480. Running Sum of 1d Array

Question

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Solution

类似动态规划,遍历数组,并将当前位置的元素和上一个位置的元素进行加和。

Code

1
2
3
4
5
6
7
8
class Solution {
public int[] runningSum(int[] nums) {
for(int i = 1; i < nums.length; i++){
nums[i] += nums[i-1];
}
return nums;
}
}
Author

Xander

Posted on

2022-06-01

Updated on

2022-06-01

Licensed under

Comments