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.
- Create a sum variable to store current sum in it ans push back it in the result array.
class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
int sum = 0;
vector<int> ans;
int n = nums.size();
for(int i=0;i<n;i++){
sum+=nums[i];
ans.push_back(sum);
}
return ans;
}
};
- We can modify input array directy
class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
int n = nums.size();
for(int i = 1;i<n;i++){
nums[i]+=nums[i-1];
}
return nums;
}
};