2015-11-27 34 views
1

我有两个数组,我需要进行排序并找到两个数组中出现的最小数字。如果没有平等,那么它应该返回-1,如果存在相等性,那么它应该返回该数字。如何在两个数组中找到共同的最小数字

这里是我的我有到目前为止的代码

public int solution(int[] A, int[] B) 
{ 
    int minA = A.Min();// Get minimum number of array A 
    int minB = B.Min();// Get minimum number of array B 

    if (minA == minB)// If both arrays have the same smallest number    
     return minA; 
    else 
     return -1; 
} 

问题是,它只是检查的最低数量的平等,如果它不匹配,那么它将返回。我如何才能看到下一个最低的数字?

回答

2

我认为解决这个问题的最好办法是将数组排序第一,你应该尝试:

public int solution(int[] A, int[] B) 
{ 
    //Sorts the array 
    Array.Sort(A) 
    Array.Sort(B) 

    //store the array positions 
    int j = 0; 
    int i = 0; 

    While(i < Array.Length(A) && j < Array.Length(B)) //If any array ends before finding the equality, the while ends and the code return -1; 
    { 
     if(A[i] == B[j]) 
     { 
      return A[i]; 
     } 

     //if the element from A is larger than the element from B, you have to go up an element in B 
     if(A[i] > B[j]) 
     { 
      j++; 
     }  

     //if the element from B is larger than the element from A, you have to go up an element in A 
     if(A[i] < B[j]) 
     { 
      i++; 
     } 
    } 

    return -1;   
} 

我没有测试过,但认为它应该工作。

+1

不错,比我的伪代码更干净。相等比较应该是==,While循环应该循环<而不是<=长度(基于零的索引)。 – PeteB

+1

谢谢。更正了代码! – forayer

0

伪代码:

sort array A in ascending order 
sort array B in ascending order 

set elementB to the first element in B 

for all elements in A 

    if (elementA == elementB) 
    return elementA 

    while (elementA > elementB) 
    try to get the next element from B 

    if there are no more elements in B 
     return -1 

    if (elementA == elementB) 
     return elementA 
    end while 

    // elementA is less than elementB 
end for 

return -1 

英文:

经过一个列表,如果它们匹配比较与B.当前值的每个值,我们就大功告成了。

如果B中的值小于A,则从B获得下一个值(因为数字从小到大排序,将不会有与当前A匹配的B值)。保持抓住下一个B值直到它等于A(我们完成)或者它比A大(我们需要抓住下一个A值来看它是否赶上新B)。

如果我们碰到任一列表的末尾,就没有匹配。

4

我认为你正在试图获得共同的最低数量(交叉口)。所以你可以在这种情况下使用Intersect

int[] arrA = {100, 102, 99, 107}; 
int[] arrB = {103, 102, 99, 105, 106, 109}; 

var commonNumbers = arrA.Intersect(arrB).ToArray(); 
return commonNumbers.Any() ? commonNumbers.Min() : -1); 
+0

+1,但是当我在本网站的回答中不需要人们调用'ToList()'的时候,我会一直使用它,否则'commonNumbers'的两个用法会做交点计算两次。将其更改为'arrA.Intersect(arrB).ToList()',它只需要实际找到相交的集合一次。 –

+0

@JonHanna:你说得对。纠正。 –

+0

'commonNumbers as int []'。是什么使这种可能性? –

1

你可以试试这个:

public int solution(int[] A, int[] B) 
{ 
    var common = A.Intersect(B).ToList(); 

    return common.Count > 0 ? common.Min() : -1; 
} 
0

的问题是寻找两个预最低,然后比较,该解决方案无需预分拣,优化CPU和内存:

public int solution(int[] A, int[] B) 
{ 
//NOTE: Assumption that the array contains at least 2 values, check & handle the different cases properly 
    //Take the first 2 values in order 
    if(A[0]<A[1]){ 
     minA=A[0]; 
     minA2=A[1]; 
    }else{ 
     minA=A[1]; 
     minA2=A[0]; 
    } 

    //Take the first 2 values in order 
    if(B[0]<B[1]){ 
     minB=B[0]; 
     minB2=B[1]; 
    }else{ 
     minB=B[1]; 
     minB2=B[0]; 
    } 

    //Select the minimum and second minimum of A 
    for(int i=0;i<A.Length;i++){ 
     if(minA>=A[i]){ 
      minA2=minA;//The previous minimum become the second minimum 
      minA=A[i]; 
     }else if(minA2>A[i]){ 
      minA2=A[i];//A[i] is the actual second minimum 
     } 
    } 
    //Select the minimum and second minimum of B 
    for(int i=0;i<B.Length;i++){ 
     if(minB>=B[i]){ 
      minB2=minB;//The previous minimum become the second minimum 
      minB=A[i]; 
     }else if(minB2>B[i]){ 
      minB2=B[i];//B[i] is the actual second minimum 
     } 
    } 

    //Do your comparison 
    return (minA==minB&&minA2==minB2)?minB2:-1; 
//NOTE: if you want to check only the second lower: return minA2==minB2?minB2:-1; 
} 
相关问题