Skip to content

Commit d346853

Browse files
authored
Merge pull request neetcode-gh#1989 from andrewmustea/1480-running-sum-of-1d-array.c
add 1480-running-sum-of-1d-array.c
2 parents a12e2ef + 921547a commit d346853

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

c/1480-running-sum-of-1d-array.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Given an array nums. We define a running sum of an array as
3+
* runningSum[i] = sum(nums[0]…nums[i]).
4+
*
5+
* Return the running sum of nums.
6+
*
7+
* Time: O(n)
8+
* Space: O(n)
9+
*/
10+
11+
int* runningSum(int* nums, int numsSize, int* returnSize){
12+
int *ret = malloc(numsSize * sizeof(int));
13+
*returnSize = numsSize;
14+
15+
for (int i = 0; i < numsSize; i++) {
16+
ret[i] = i ? nums[i] + ret[i-1] : nums[i];
17+
}
18+
19+
return ret;
20+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy