303
This commit is contained in:
44
src/303.c
Normal file
44
src/303.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#define PREFIX
|
||||
|
||||
#include <solution/303.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef PREFIX
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
NumArray *numArrayCreate(int *nums, int numsSize)
|
||||
{
|
||||
NumArray *ret = malloc(sizeof(NumArray));
|
||||
ret->size = numsSize;
|
||||
#ifdef PREFIX
|
||||
ret->data = malloc(sizeof(int) * (numsSize + 1));
|
||||
ret->data[0] = 0;
|
||||
for (int i = 0; i < numsSize; i++)
|
||||
ret->data[i + 1] = ret->data[i] + nums[i];
|
||||
#else
|
||||
ret->data = malloc(sizeof(int) * numsSize);
|
||||
memcpy(ret->data, nums, sizeof(int) * numsSize);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
int numArraySumRange(NumArray *obj, int left, int right)
|
||||
{
|
||||
#ifdef PREFIX
|
||||
return obj->data[right + 1] - obj->data[left];
|
||||
#else
|
||||
int sum = 0;
|
||||
for (int i = left; i <= right; i++)
|
||||
sum += obj->data[i];
|
||||
|
||||
return sum;
|
||||
#endif
|
||||
}
|
||||
|
||||
void numArrayFree(NumArray *obj)
|
||||
{
|
||||
free(obj->data);
|
||||
free(obj);
|
||||
}
|
||||
Reference in New Issue
Block a user