Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1989 from andrewmustea/1480-running-su…
Browse files Browse the repository at this point in the history
…m-of-1d-array.c

add 1480-running-sum-of-1d-array.c
  • Loading branch information
tahsintunan authored Jan 11, 2023
2 parents a12e2ef + 921547a commit d346853
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions c/1480-running-sum-of-1d-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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.
*
* Time: O(n)
* Space: O(n)
*/

int* runningSum(int* nums, int numsSize, int* returnSize){
int *ret = malloc(numsSize * sizeof(int));
*returnSize = numsSize;

for (int i = 0; i < numsSize; i++) {
ret[i] = i ? nums[i] + ret[i-1] : nums[i];
}

return ret;
}

0 comments on commit d346853

Please sign in to comment.