楼主:
oin1104 (是oin的说)
2023-11-25 13:48:06我的想法也差不多捏
直接把他当成数线上面的点
然后
往前移动的数量乘上后面有几个点
就可以知道要减多少了
加也是一样道理
捏
```c
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getSumAbsoluteDifferences(int* nums, int numsSize, int* returnSize)
{
int all = 0 ;
for(int i = 0 ; i < numsSize ; i ++)
{
all += nums[i];
}
*returnSize = numsSize;
int *res = malloc(sizeof(int) * (numsSize+1));
for(int j = 0 ; j < numsSize ; j ++)
{
if(j > 0)
{
all = all - ((nums[j] - nums[j-1]) * (numsSize - j )) + ((nums[j]- n
ums[j-1]) * j );
}
else
{
all = all - ((nums[j]) * (numsSize - j)) + ((nums[j]) * j );
}
res[j] = all;
}
return res;
}
```