我正在创建一个可以比较两个数组的函数。当它们相同时返回1,当它们不相同时返回0.
它要求程序以线性时间运行,所以我不能使用for-for循环来比较它。对我有什么建议吗?
scrambled应返回1的数组示例:
a = {10,15,20}, b = {10,15,20}
a = {1,2,3,4,5}, b = {5,3,4,2,1}
a = {}, b = {} (i.e. len = 0)
a = {2,1,3,4,5}, b = {1,2,4,3,5}
scrambled应返回的数组示例为0:
a = {1,1}, b = {1,2}
a = {10,15,20}, b = {10,15,21}
a = {1,2,3,4,5}, b = {5,3,4,2,2}
答案 0 :(得分:2)
如果你可以为数组元素指定一个最大值,你可以很容易地在线性时间内比较它们,只需循环遍历每个元素并计算存在的值,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_ARRAY_VALUE 100
bool compare_arrays(int * arr1, size_t arr1_size,
int * arr2, size_t arr2_size)
{
/* Set up array to count elements */
int * table = calloc(MAX_ARRAY_VALUE + 1, sizeof * table);
if ( !table ) {
perror("couldn't allocate memory");
exit(EXIT_FAILURE);
}
/* Increment index if element is present in first array... */
for ( size_t i = 0; i < arr1_size; ++i ) {
table[arr1[i]]++;
}
/* ...and decrement index if element is present in second array. */
for ( size_t i = 0; i < arr2_size; ++i ) {
table[arr2[i]]--;
}
/* If any elements in array are not zero, then arrays are not equal */
for ( size_t i = 0; i < MAX_ARRAY_VALUE + 1; ++i ) {
if ( table[i] ) {
free(table);
return false;
}
}
free(table);
return true;
}
int main(void) {
int a1[] = {10, 20, 30, 10};
int a2[] = {20, 10, 10, 30};
int a3[] = {1, 4, 5};
int a4[] = {1, 3, 5};
if ( compare_arrays(a1, 4, a2, 4) ) {
puts("a1 and a2 are equal"); /* Should print */
}
else {
puts("a1 and a2 are not equal"); /* Should not print */
}
if ( compare_arrays(a3, 3, a4, 3) ) {
puts("a3 and a4 are equal"); /* Should not print */
}
else {
puts("a3 and a4 are not equal"); /* Should print */
}
if ( compare_arrays(a1, 4, a4, 3) ) {
puts("a1 and a4 are equal"); /* Should not print */
}
else {
puts("a1 and a4 are not equal"); /* Should print */
}
return 0;
}
输出:
paul@horus:~/src/sandbox$ ./lincmp
a1 and a2 are equal
a3 and a4 are not equal
a1 and a4 are not equal
paul@horus:~/src/sandbox$
如果不指定最大值,则可以遍历每个数组并找到最大值。它仍然是线性时间,但如果没有上限,你最终可能会得到一个巨大的索引表。
答案 1 :(得分:1)
Because the comparison of the two arrays is independent of the order of the elements, both must be sorted before they can be compared. Because of this, you can't do this in linear time. The best you can do is O(n log n)
, as that is the best order of most sorting algorithms.
答案 2 :(得分:0)
我的C很生疏,所以下面的脚本可能会出现内存问题。但是,基本任务是对2个数组进行排序,逐个元素地进行比较。如果它们都匹配,则scramble
应返回1,其他为0。
下面的脚本适用于GCC,还没有使用任何其他编译器对其进行测试。它比较相等长度的数组。不等长的情况留给OP作为一项小练习。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare_ints(const void * a , const void * b)
{
const int * ia = (const int *)a;
const int * ib = (const int *)b;
return *ia > *ib;
}
int scramble(const int * a, const int * b, unsigned int len)
{
int * sorted_a = malloc(len * sizeof(int));
int * sorted_b = malloc(len * sizeof(int));
memcpy(sorted_a, a, len * sizeof(int));
memcpy(sorted_b, b, len * sizeof(int));
qsort(sorted_a, len, sizeof(int), compare_ints);
qsort(sorted_b, len, sizeof(int), compare_ints);
for (int i = 0; i < len; i++)
{
if (sorted_a[i] != sorted_b[i])
{
free(sorted_a);
free(sorted_b);
return 0;
}
}
free(sorted_a);
free(sorted_b);
return 1;
}
int main (int argc, char const *argv[])
{
int a[3] = {20, 10, 15};
int b[3] = {10, 15, 20};
int is_equal = scramble(a, b, 3);
printf("is_equal = %i\n", is_equal);
return 0;
}
仅供参考:您无法在线性时间内完成此操作。 qsort
有O(n log n)
。
答案 3 :(得分:0)
在某种程度上,您可以获得第一个数组的所有散列值的总和,并将其与第二个数组的散列值的总和进行比较阵列。它会起作用,但我不确切知道它。
这是我的尝试,到目前为止我的所有测试都给出了积极的结果:
[String : AnyObject]