什么是有效的方式来获得两个数组的差异用C? [英] What is the efficient way to get the diff of two arrays in C?
问题描述
采样输入
Array_1 [] = { 10, 20, 30, 40, 50 };
Array_2 [] = { 30, 40, 50, 60, 70 };
示例输出
Array_1_Extras [] = { 10, 20 };
Array_2_Extras [] = { 60, 70 };
说明
-
输入数组排序。
Input Arrays are not sorted.
输入数组长度 - 10K
Input Arrays Length - 10K.
输入数组值的范围0 - 4095
Input Array values range 0 - 4095.
输入数组订单和值不应被修改。
Input Array orders and values should not be modified.
输出数组可以在任何顺序。
Output Arrays can be in any order.
重复没有必要在输出数组。
Duplicates not needed in the Output Arrays.
我需要为这个项目有一段时间有效的逻辑。提前致谢。
I need some time efficient logic for this program. Thanks in advance.
推荐答案
下面是一个简单的 O(N)
时间算法有点浪费空间(它正在帐户重复以及)。
Here is a simple O(N)
time algorithm somewhat space consuming (it is taking in account duplicates as well).
1)声明数组 C
长度零 4096
,结果的
2)迭代上的第一阵列 A
和索引的每个元素 I
的增加的 C [A [I]]
结果
3)迭代第二阵列 B
,并为每个元素 I
的减的 C [B〔I]]
结果
4)迭代 C
和 I
负 C [I]
将指明 B的元素 - A
,和积极的人会指示 A的元素 - B
< BR>
*和绝对值将表明,在受骗者数之差,如果你有兴趣。
1) Declare an array C
of zeros of length 4096
.
2) Iterate on first array A
and for each element indexed i
increment C[A[i]]
3) Iterate second array B
, and for each element i
decrement C[B[i]]
4) Iterate over C
and i
of negative C[i]
will indicate an element of B - A
, and of positive one will indicate an element of A - B
* And the absolute value will indicate the difference in the dupes number, if you are interested..
这篇关于什么是有效的方式来获得两个数组的差异用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!