如何在两个数组中找到共同的最小数
本文关键字:小数 数组 两个 | 更新日期: 2023-09-27 18:24:35
我有两个数组,需要对它们进行排序,并找到两个数组中出现的最低数字。如果不存在相等,那么它应该返回-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;
}
问题是,它只检查最低数字的相等性,如果不匹配,则返回。我怎样才能得到下一个最低的数字?
我认为您正在尝试获得公共最小数(交集)。所以在这种情况下可以使用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);
我认为解决这个问题的最佳方法是首先对数组进行排序,您应该尝试:
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;
}
我没有测试,但认为它应该有效。
你可以试试这个:
public int solution(int[] A, int[] B)
{
var common = A.Intersect(B).ToList();
return common.Count > 0 ? common.Min() : -1;
}
在伪代码中:
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)。
如果我们到达了任一列表的末尾,则没有匹配项。
问题是搜索预最小值,然后进行比较,这个解决方案没有预排序,优化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;
}
这是我在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