-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp303.c
38 lines (34 loc) · 817 Bytes
/
p303.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
typedef struct {
int *sum;
} NumArray;
NumArray* numArrayCreate(int* nums, int numsSize) {
NumArray *result = (NumArray*)malloc(sizeof(NumArray));
result->sum = (int*)malloc(sizeof(int)*numsSize);
int i;
result->sum[0] = nums[0];
for (i = 1; i < numsSize; i++)
{
result->sum[i] = result->sum[i-1]+nums[i];
}
return result;
}
int numArraySumRange(NumArray* obj, int i, int j) {
if (i == 0)
{
return obj->sum[j];
}
else
{
return obj->sum[j]-obj->sum[i-1];
}
}
void numArrayFree(NumArray* obj) {
free(obj->sum);
free(obj);
}
/**
* Your NumArray struct will be instantiated and called as such:
* struct NumArray* obj = numArrayCreate(nums, numsSize);
* int param_1 = numArraySumRange(obj, i, j);
* numArrayFree(obj);
*/