leetcode链接:一维数组的动态和
ps:
动态数组求和其实就是当前 i
位置的值等于 0 - i
的求和,控制好循环条件即可。
代码:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* runningSum(int* nums, int numsSize, int* returnSize){
*returnSize = numsSize;
int * ans = (int*)calloc(numsSize , sizeof(int));
for (int i = 0; i < numsSize; i++) {
int j = 0;
for (j = 0; j <= i; j++) {
ans[i] += nums[j];
}
}
return ans;
}