Re: [閒聊] 每日LeetCode
我的想法也差不多捏
直接把他當成數線上面的點
然後
往前移動的數量乘上後面有幾個點
就可以知道要減多少了
加也是一樣道理
捏
```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;
}
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 134.208.57.64 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1700891288.A.E63.html
推
11/25 13:49,
2年前
, 1F
11/25 13:49, 1F
→
11/25 13:49,
2年前
, 2F
11/25 13:49, 2F
推
11/25 13:51,
2年前
, 3F
11/25 13:51, 3F
→
11/25 13:51,
2年前
, 4F
11/25 13:51, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 539 之 719 篇):