我正在尋找解決以下找到添加算法/在數組中刪除的元素
問題的最efficent方式:
given an array Before = { 8, 7, 2, 1} and an array After ={1, 3, 8, 8}
find the added and the removed elements
the solution is:
added = 3, 8
removed = 7, 2
我的想法至今:
for i = 0 .. B.Lenghtt-1
{
for j= 0 .. A.Lenght-1
{
if A[j] == B[i]
A[j] = 0;
B[i] = 0;
break;
}
}
// B elemnts different from 0 are the Removed elements
// A elemnts different from 0 are the Added elemnts
有沒有人知道更好的解決方案,也許更高效,並且不會覆蓋原始陣列
如果3,8被添加和7,圖2,1被移除,則「數組後」應該是'{3,8,8}'或'{1,3,8,8}'。 – kennytm 2010-05-04 11:14:33
「After」數組中不能存在「1」。你從一個「1」開始,你刪除它,並且不添加它,那麼爲什麼它在After數組中?你應該修復你的例子。 – 2010-05-04 12:43:41
我平時的錯誤,我已經糾正它。 Thx,jj – 2010-05-04 13:59:48