Re: [閒聊] 每日LeetCode

看板Marginalman作者 (是oin的說)時間2年前 (2023/11/25 13:48), 編輯推噓2(202)
留言4則, 3人參與, 2年前最新討論串539/719 (看更多)
我的想法也差不多捏 直接把他當成數線上面的點 然後 往前移動的數量乘上後面有幾個點 就可以知道要減多少了 加也是一樣道理 捏 ```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
文章代碼(AID): #1bOOgOvZ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bOOgOvZ (Marginalman)