我正在寻找解决以下问题的最有效方法:
问题:
我的想法到目前为止是:
有人知道一种更好的解决方案吗?也许更有效,并且不会覆盖原始数组。
问题:
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, 8}
或{1, 3, 8, 8}
。 - kennytm