Running Sum of 1d Array problem results
First Blood
My first solution is so straightforward, that I created a new vector to save the running sum
1
2
3
4
5
6
7
8
9
10
11
12
13
{
vector<int> runningSum(vector<int>& nums)
{
vector<int> runningSum(nums.size());
runningSum[0] = nums[0];
for(int i = 1; i < nums.size(); i++)
{
runningSum[i] = runningSum[i-1] + nums[i];
}
return runningSum;
}
}
Double Kill
It could be better, I can return the result with the input array.
1
2
3
4
5
6
7
8
9
10
11
12
{
vector<int> runningSum(vector<int>& nums)
{
for(int i = 1; i < nums.size(); i++)
{
nums[i] += nums[i-1];
}
return nums;
}
}