Skip to content

Commit e5d2652

Browse files
Update and rename RunningSum.cpp to easy-1480-running_sum_of_1d_array.cpp
1 parent 1a3c208 commit e5d2652

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

Codes/RunningSum.cpp

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given an array nums, we define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
2+
3+
// Return the running sum of nums.
4+
5+
6+
7+
// Example 1:
8+
9+
// Input: nums = [1,2,3,4]
10+
// Output: [1,3,6,10]
11+
// Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
12+
13+
// Example 2:
14+
15+
// Input: nums = [1,1,1,1,1]
16+
// Output: [1,2,3,4,5]
17+
// Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
18+
19+
// Example 3:
20+
21+
// Input: nums = [3,1,2,10,1]
22+
// Output: [3,4,6,16,17]
23+
24+
25+
class Solution {
26+
public:
27+
vector<int> runningSum(vector<int>& nums) {
28+
vector<int> sum;
29+
sum.push_back(nums[0]);
30+
int n=0;
31+
for(int i=1;i<nums.size();i++){
32+
sum.push_back(sum[i-1]+nums[i]);
33+
}
34+
return sum;
35+
}
36+
};
37+
/*
38+
0ms solution
39+
40+
class Solution {
41+
public:
42+
vector<int> runningSum(vector<int>& nums)
43+
{
44+
int n = nums.size();
45+
for(int i = 1; i < n; i++)
46+
nums[i] += nums[i-1];
47+
48+
return nums;
49+
}
50+
};
51+
52+
*/

0 commit comments

Comments
 (0)