代碼實現如下:
- 思路
- 定義三個變量,在三個數組中,由後向前遍歷
- 代碼待完善
- 數組是否合法,未檢查
- 當查找不到時的返回值爲 -1 ,有待商榷
public class Test {
public static void main(String args[])
{
int[] a = new int[]{3,4,5,6,7,8,9,10,11};
int[] b = new int[]{5,6,7,8,9};
int[] c = new int[]{6,7,8,9,10};
System.out.println(Test.findMaxCommon(a,b,c));
}
public static int findMaxCommon(int[] x,int[] y,int[] z)
{
int i,j,k;
i = x.length-1;
j = y.length-1;
k = z.length-1;
while(i>0 && j>0 && k>0)
{
if(x[i] > y[j]) i--;
else if(y[j] > z[k]) j--;
else if(z[k] > x[i]) k--;
else
{
return x[i];
}
}
return -1;
}
}