精華區beta Marginalman 關於我們 聯絡資訊
今天這題我卡超級久 我也是差不多的想法 是別人偷偷地跟我說的 而且我是被其他人教才寫成功的 所以這水分超多 int* restoreArray(int** adjacentPairs, int adjacentPairsSize, int* adjacentPairs ColSize, int* returnSize) { int map[200001][3] ={}; // [0]=-100000 [100000]=0 [200000]=100000 // map(數字+100000) [玲居1][玲居2][有幾個] int *ans = malloc(sizeof(int) * (adjacentPairsSize+1)); *returnSize = adjacentPairsSize + 1 ; for(int i = 0 ; i < adjacentPairsSize ; i++) { //全部先塞進map if( map[adjacentPairs[i][0]+100000][2] == 1 ) { map[adjacentPairs[i][0]+100000][1] = adjacentPairs[i][1]; } else { map[adjacentPairs[i][0]+100000][0] = adjacentPairs[i][1]; } map[adjacentPairs[i][0]+100000][2] ++; if( map[adjacentPairs[i][1]+100000][2] == 1 ) { map[adjacentPairs[i][1]+100000][1] = adjacentPairs[i][0]; } else { map[adjacentPairs[i][1]+100000][0] = adjacentPairs[i][0]; } map[adjacentPairs[i][1]+100000][2] ++; } //只有一個的 一定是端點 for(int j = 0 ; j < 200001 ; j++) { if(map[j][2] == 1) { ans[0] = j - 100000; ans[1] = map[ans[0]+100000][0]; break; } } for(int k = 1 ; k < adjacentPairsSize ; k++) { if(ans[k-1] == map[ans[k]+100000][0]) { ans[k+1]=map[ans[k]+100000][1]; } else { ans[k+1]=map[ans[k]+100000][0]; } } return ans; } 就是像這樣 假設有(2,3) (1,2) (3,4) 那就建個表格紀錄他們的 數字 - 鄰居 - 鄰居 - 出現數 1 - 2 2 - 3 1 3 - 2 4 4 - 1 只出現一次的就是端點 然後再依據前面是誰 跟現在是誰 來看下一個是誰 然後 很抽象 很麻煩 我吐了 晚安 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.128.26 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1699640926.A.DBE.html
Rushia: 晚安11/11 02:29
oin1104: 晚安11/11 02:30
wwndbk: 你的code好像比較簡潔11/11 02:30
oin1104: 我的code雞八難懂 我自己都快看不懂 11/11 02:31
※ 編輯: oin1104 (61.230.128.26 臺灣), 11/11/2023 02:37:48
dustsstar79: 大師 11/11 03:30
dustsstar79: c++的話用STL會更好寫 11/11 03:30
Neuenmuller: 大師,C code欸 11/11 07:49