什么是有效的方式来获得两个数组的差异用C? [英] What is the efficient way to get the diff of two arrays in C?

查看:150
本文介绍了什么是有效的方式来获得两个数组的差异用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 };

说明


  1. 输入数组排序。

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆