본문 바로가기

IT/코딩테스트

[Leetcode] 1480. Running Sum of 1d Array

728x90

https://leetcode.com/problems/running-sum-of-1d-array/

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* runningSum(int* nums, int numsSize, int* returnSize){

    int* result = (int *)malloc(sizeof(int) * numsSize);
    memset(result, 0, sizeof(int) * numsSize);
    *returnSize = numsSize;

    for (int i=0; i < numsSize; i++) {
        for (int j=0; j <= i; j++) {
            *(result + i) += *(nums + j);
        }
    }

    return result;
}
728x90