如何在两个数组

时间:2015-11-27 03:46:20

标签: c# arrays sorting

我有两个数组需要排序并找到两个数组中出现的最小数字。如果没有相等性,那么它应该返回-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;
}

问题是它只检查最低数字的相等性,如果它不匹配则返回。我怎样才能看到下一个最低的数字?

6 个答案:

答案 0 :(得分: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);

答案 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;         
}

我没有测试过,但认为它应该可行。

答案 2 :(得分:1)

你可以试试这个:

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

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

答案 3 :(得分: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

英文:

浏览A列表并将每个值与B中的当前值进行比较。如果匹配,我们就完成了。

如果B中的值小于A,则从B获取下一个值(因为数字从小到大排序,将永远不会有与当前A匹配的B值。)

继续抓住下一个B值,直到它等于A(我们已经完成)或者它大于A(我们需要抓住下一个A值以查看它是否赶上新B)。

如果我们点击任一列表的末尾,则没有匹配。

答案 4 :(得分: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;
}

答案 5 :(得分:0)

这是我在python中的解决方案。

#Solution 1
def solution(A, B):
    commonList = [n for n in B if n in A]
    commonList.sort()
    if len(commonList) > 0:
        return commonList[0]
    return -1

#Solution 2
def solution(A, B):
    commonList = []
    for n in A: #Iterates through A and check the elements that appear in B
        if n in B:
            commonList.append(n)
    commonList.sort() 
    #Sort list in order to get minimum value in the first index. 
    #you could use min(commonList) too
    if len(commonList) > 0:
        return commonList[0]
    return -1