public static int[] Func(int[] m, int[] n)
{
if (m == null || n == null)
{
throw new ArgumentException("传入数组不能为空");
}
int[] result = new int[m.Length + n.Length];
int mIndex = 0;
int nIndex = 0;
for (int index = 0; index < result.Length; index++)
{
if (mIndex >= m.Length)
{
result[index] = n[nIndex];
continue;
}
if (nIndex >= n.Length)
{
result[index] = m[mIndex];
continue;
}
if (m[mIndex] < n[nIndex])
{
result[index] = m[mIndex];
mIndex++;
}
else
{
result[index] = n[nIndex];
nIndex++;
}
}
return result;
}
本文介绍了一个简单的数组合并算法,该算法将两个整数数组合并为一个有序数组。通过比较两个输入数组中的元素,逐个填充结果数组,确保结果数组中的所有元素按升序排列。
1102

被折叠的 条评论
为什么被折叠?



