0

我有一个由 25 个数字组成的数组,我还有另一个由 15 个数字组成的数组。在 25 个数字数组中的 15 个数字中准确找到(不多也不少)5 个匹配项的最快方法是什么?

例子:

int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }

int[] array2 = { 1, 2, 3, 4, 5, 36, 77, 88, 49, 50, 51, 52, 53, 54, 55 }

int[] array3 = { 1, 2, 3, 4, 5, 6, 26, 17, 28, 29, 30, 31, 32, 33, 34 }

int[] array4 = { 1, 2, 3, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 }

在示例中,array2 是有效数组,因为它恰好有 5 个匹配项,但 array3 和 array4 无效。

4

3 回答 3

2
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
int[] array2 = { 1, 2, 3, 4, 5, 36, 77, 88, 49, 50, 51, 52, 53, 54, 55 };

int numMatches = array1.Intersect(array2).Count();
于 2013-08-15T19:22:30.303 回答
0
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };

int[] array2 = { 1, 2, 3, 4, 5, 36, 77, 88, 49, 50, 51, 52, 53, 54, 55 };

int[] array3 = { 1, 2, 3, 4, 5, 6, 26, 17, 28, 29, 30, 31, 32, 33, 34 };

int[] array4 = { 1, 2, 3, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };

var matches = 
     from set1 in array1 
     join set2 in array2 on set1 equals set2
     select set1;
 if (matches.Count() == 5)
        return true;
 else
       return false;
于 2013-08-15T18:51:55.030 回答
0

这种方法并没有做太多的工作:

bool CompareCount(int[] array1, int[] array2, int count)
{
    // Check that the arrays contain at least count of integers.
    if (array1 == null || array1.length < count) return false;
    if (array2 == null || array2.length < count) return false;

    // Check that the first count integers are identical.
    for(int i = 0; i < count; count++)
       if (array1[i] <> array2[i]) return false;
    // If one of the arrays only contains count integers, then its okay.
    if (array1.length == count || array2.length == count) return true;

    // If the next integers are different, then its okay. 
    return array1[count] != array2[count];
}
于 2013-08-15T19:50:53.817 回答